Reputation: 1339
When my program is being closed (initiated by Application.Shutdown()), I'm catching the Closing event on the main window. In the handler, I create a dialog box asking the user if they want to save their work. In this specific case, the user cannot cancel the application closing event, which is fine by me.
I have found that if I initiate the "want to save" Window via ShowDialog, the window never shows. However, MessageBox.Show does show a window. Is this expected behavior during application shutdown? I can't find anything in the documentation about this behavior. If someone could point me to the relevant documentation or a detailed answer to this question, that would be great.
Upvotes: 2
Views: 407
Reputation: 853
If you use Application.Current.MainWindow.Close();
that will allow you to use
e.Cancel = true;
, after which if you use Application.Current.Shutdown();
it will ignore the cancel flag and close the window.
This is assuming you're only using one window, and of course will need to not show your prompt a second time.
Something like:
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
DisplayCloseConfirmation();
}
Edited based on comments
Upvotes: 1
Reputation: 3880
Application.Shutdown ultimately stops the message loop (Dispatcher) for your application. Nothing UI will occur after that. The only reason MessageBox.Show works is because it creates its own internal message loop on the active window of the application.
Upvotes: 4