private void FormFormClosingEventHandler(object sender, FormClosingEventArgs e)
{
switch (e.CloseReason)
{
case System.Windows.Forms.CloseReason.ApplicationExitCall:
//When there is a "fatal exception", we terminate the gui toolkit, which calls Application.Exit().
//So, we can't cancel the close, otherwise the application can get into a funny state.
return;
case System.Windows.Forms.CloseReason.TaskManagerClosing:
case System.Windows.Forms.CloseReason.WindowsShutDown:
// windows is trying close the application, not just this window
// rather than propagate the request to close this window, we need
// to ask the entire desktop to quit
Application.Quit();
break;
case System.Windows.Forms.CloseReason.UserClosing:
case System.Windows.Forms.CloseReason.None:
// notify the model that a close request was made
RaiseCloseRequested();
break;
default:
// other close reasons are not applicable
break;
}
// cancel the request - don't let winforms close the form
e.Cancel = true;
}