we have an application that needs to save settings when shutting down, including when windows shuts down. We are successfully receiving the Application.Exit
event in this case, but then, depending on timing the application is simply terminated. It is not like we are doing a lot more than saving two small XML files with settings. Sometimes they get saved, sometimes not, again all depending on timing.
Is there some maximum amount of time allowed for the application to clean up after receiving a signal that windows is shutting down?
The following work-around "works" but I do not understand why:
// In our main application class derived from Application:
protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
{
e.Cancel = true;
this.Shutdown();
base.OnSessionEnding(e);
}
This seems to work fine, and allows the application to exit in the same way as it would normally do (it is a minimize-to-tray type of app that uses ShutdownMode.OnExplicitShutdown
and when quitting from the tray icon we do call Application.Shutdown explicitly).
So why does windows not allow time for the application to handle the Exit event? What causes it to force kill the application with seemingly no wait time? Is it because we have no open window left (we hide our main window when it gets minimized to the tray but do not close it)?
What about the work-around? Is it bad practice, or okish?