You’ll typically handle DispatcherUnhandledException in order to handle unforeseen exceptions in a more friendly manner. After doing something with the exception information, e.g. logging it or notifying the user, you typically have the following options.

Ignore cause of exception and continue execution

Set e.Handled to true

Allow unhandled exception to crash application as it normally would

Leave e.Handled set to false

Shut down gracefully (typical behavior)

Set e.Handled to true

Invoke Application.Shutdown()

You’ll most often select the third option. If you’re logging or reporting exception information in a friendly manner, you will want to mark Handled as true. And you do typically want to shut down and application after something unexpected happens. Often you don’t know what state your application is in, so it’s desirable to just exit.

By default, a WPF application terminates when all of its windows are closed. This corresponds to a value of OnLastWindowClose for the main Application object’s ShutdownMode property.

You can also set the ShutdownMode property to OnExplicitShutdown. This indicates that the application will not terminate even after the user closes all of its windows. Instead, you need to explicitly exit the application from within your code.

By default, a WPF application terminates when all of its windows are closed. This corresponds to a value of OnLastWindowClose for the main Application object’s ShutdownMode property.

You can also set the ShutdownMode property to OnMainWindowClose. This indicates that the application should terminate when the main window closes. Any other windows that are currently open will be automatically closed as the application terminates.

The application’s main window is the one specified using the Application object’s StartupUri property. Note that the MainWindow property of the main Application object will refer to the instance of the window created based on the StartupUri.

At this point, we can close either window and the application will continue running. It will only terminate when we close the second of the two open windows. If we use the “Open Another” button to open multiple windows, the application will only terminate after we close the last open window. The order that we close the windows doesn’t matter.