CancelClose event - Detecting user close vs. System close

Hi all,

My app asks for confirmation before closing the main program window via the CancelClose event. A “Are you sure you want to quit?” msgbox.

Is there a way to detect if the close is due to a system shutdown? Because what is happening now is that if the user tries to Restart or Shutdown their computer while my application is running, they are still prompted with this message box which hangs the shutdown until it gets an answer.

I’d like to detect if the close is due to a system shutdown so that I can omit the msgbox in this case.

Any ideas?

Nothing built-in that I am aware of.

The Monkeybread Software plugin has the WindowsPowerStateMBS class has events that will fire if the computer is going to sleep or being shutdown. WakeNotifierMBS is the equivalent on the Mac side. Once the event fires you can do your own preparations.

IMHO the dialog box “Are you sure you want to quit?” should not be used anymore. Re-starting an application does not take long as it did 20 years ago on Window 3.1 or the like – and this was the reason for this dialog box as far as I know.

What is necessary is a dialog box “Are you sure you want to discard the changes?” for each window of an application should the fields have been changed by the user and not yet been saved. And this dialog should of course block the system shutdown.

In general (at least in my case) a system shutdown is due to an Unhandled Exception.
This can be detected by the App event “UnhandledException”. There I use to ask what Eli suggests. There is no problem in doing it before the system halts the application.

[quote=336214:@Ramon SASTRE]In general (at least in my case) a system shutdown is due to an Unhandled Exception.
This can be detected by the App event “UnhandledException”. There I use to ask what Eli suggests. There is no problem in doing it before the system halts the application.[/quote]

I was not able to capture a restart in the UnhandledException event. In my simple test app the system just ended the application.

Agreed for most apps, but for an app that provides continuous, system-wide functionality a close confirmation is still worth having.

Thanks for this. Will look into it.

In the CancelClose event there is a parameter which will tell you pretty reliably.

If the app is closing because the user chose ‘Quit’ or the system is shutting down, appQuitting will be true.
You can still choose to block the shutdown if you have changes to be saved.
But if appQuitting is true, dont bother asking ‘did you mean to do that?’ if there are no changes to be saved.

Unless you use a recent OS version AND add the appropriate code on quit and launch.

Or you add a method that store the Windows (Documents in Windows), each window contents to be saved on disk on quit / open back at application open time (Serialization ?)

Was AutoSave the marketing name for these OS (macOS / Windows 10, Linux ?) implemented technologies ?

[quote=336235:@Jeff Tullin]In the CancelClose event there is a parameter which will tell you pretty reliably.

If the app is closing because the user chose ‘Quit’ or the system is shutting down, appQuitting will be true.
You can still choose to block the shutdown if you have changes to be saved.
But if appQuitting is true, dont bother asking ‘did you mean to do that?’ if there are no changes to be saved.[/quote]

Great! This does the trick. Thanks so much!