Search code examples
c#wpfshowdialogapplication-shutdown

After Application.Shutdown() is called, showing a window using ShowDialog doesn't work


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.


Solution

  • 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.