Question Detecting MDI application shutdown

Administrator

VB.NET Forum Admin
Joined
Jun 3, 2004
Messages
1,462
Programming Experience
10+
I typically prompt the user to save changes in the FormClosing event in MDI children. The application updater uses a Process.CloseMainWindow call to close the application if it's running before the patch update is applied.

I need to detect when the updater is closing the application and NOT prompt the user to save changes but save them automatically. I've tried using the System.Windows.Forms.CloseReason but this method isn't working for me. I capture the CloseReason in the MDI Parent FormClosing event but it's called AFTER the MDI children are told to close. I tried capturing the CloseReason (e.CloseReason) in the MDI child's FormClosing event but it registers as MDI Parent closing and a normal application exit is also going to raise this same e.CloseReason so I can't auto-save here either.

So my question is: How can I detect this Process.CloseMainWindow being used to close my application so that I can bypass all save prompts and save automatically? What is the first event called in this method of closing so that I can signal all MDI children that are open in their FormClosing event to just save?

Thanks!
 
I can't think of a simple way to do that. I can think of a somewhat messy way but even that might not work. You could set a flag when the app is closed by an internal mechanism and then you can test that flag in your FormClosing event handlers. If it's not set then the closure is by an external mechanism and you don't prompt.

The thing is, you'd have to detect when the user clicks the Close button on the title bar or uses the system menu to close, which you'd probably do in the WndProc method. I'm just not sure that a call to Process.CloseMainWindow won't send the same message(s), so you still wouldn't be able to distinguish the different closure reasons. Worth a try though.
 
Back
Top