tags:

views:

39

answers:

2

I have the following line in my Window stanza in the XAML file:

    Closing="Window_Closing"

I originally thought that the web page here assured me that this event fires when you use the big X close method, Alt-F4 or close from the system menu yet that doesn't appear to be the case since my breakpoint on the Window_Closing() function isn't being hit.

It does hit the breakpoint when I do the File, Exit method of exiting so that's working okay.

Re-reading that linked page leads me to believe that it may not trigger the closing event.

My questions are:

  1. How do you catch the three methods listed in order to detect if your file is dirty and needs saving? I have all the 'dirty' and file saving code done, I just need to know how to trap the events.

  2. Can you stop the exit from taking place with that method (as you can by intercepting the closing event)? In other words, if the user says they don't want to exit because the accidentally used Alt-F4 on the wrong window, can it be done?

+2  A: 

According to the documentation page for the Closing event:

If a session ends because a user logs off or shuts down, Closing is not raised; handle SessionEnding to implement code that cancels application closure.

Therefore you'll want to make sure you handle the SessionEnding event as well as the Closing event. The SessionEnding event could be used to automatically save the current state to a temporary file that will be loaded again the next time the application starts. But if you do want to prompt the user, you can do so with a modal dialog box in SessionEnding but they will likely see the Windows screen that warns about unresponsive applications, giving them the chance to kill the process without responding to your dialog.

Josh Einstein
Thanks, Josh. Turns out it _was_ executing the code for those three events since it popped up a messagebox when I added it to debug. I'm not sure if there was something wrong with my breakpoint (or vs2008 in general). I'll accept this answer though since you've given me a solution to a problem I didn't even know I had yet :-)
paxdiablo
Sorry I misinterpreted your title to mean "system shutting down" as opposed to closing from the system menu.
Josh Einstein
A: 

Try the Closed event instead: Closed="Window_Closing"

Bermo
Thanks, Bermo, but I wanted to be able to stop the exit which I don't think you can do once the closed event comes in.
paxdiablo
Sorry, didn't read the question close enough (sorry about the pun - couldn't help myself!).
Bermo