c# - Closing all windows in WPF after one has been closed -


is there way close windows after user closes 1 of them, , here important part: without using app.current.shutdown(), rather invoking close on remaining windows individually?

my idea invoke close on each of remaining windows in window_closing event handler method. there's 1 problem, though. suppose have 2 window classes, a , b. create 1 instance of a , b - a , b respectively. if close window a, invokes window_closing event handler method , calls b.close() there. in b class (a , b window classes, both inherit window) window_closing method invoked (because i've called b.close()), , b::window_closing calls a.close() , results in exception cause i've closed a.

what right way solve this?

if interested in having "main window" , "tool windows", closing main window closes of them , closing tool windows does, well, - in app.xaml there's friendly option that!

that's application.shutdownmode

<application     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      startupuri="mainwindow.xaml"     shutdownmode="onmainwindowclose"     > </application> 

note here, "mainwindow" in question, the main window application starts (or 1 set main, if played during app's lifetime).

edit: afterthought: of course close windows, i'm not sure if calls normal "close", or shutsdown whole application. sorry, you'd need check yourself. if you're interested in opinion, that's way should/could easily, , if really-really-need shutdown app "close()"ing every window, sense you're doing wrong here, if remember correctly, "window.close()" may cancelled.

edit2: yup, window.close() can cancelled. please see article:

closing window causes closing event raised. if closing event isn't canceled, following occurs: (...)

so looping on window collection , calling 'close' doesn't guarantee windows closed , app may still left running afterwards.


Comments