Application ShutdownMode in WPF

ShutdownMode property gets or sets the condition that causes the Shutdown method to be called. Shutdown method is responsible for shutting down the application. This is a type of ShutdownMode enumeration and has following three values:

  • OnLastWindowClose - An application shuts down when either the last window closes, or Shutdown method of application is called.
  • OnMainWindowClose - An application shuts down when either the main window closes, or Shutdown method of application is called.
  • OnExplicitShutdownAn application shuts down only when Shutdown method of application is called.

The code snippet in Listing 1 sets ShutdownMode at design-time inXAML.

<Application x:Class="WPFSample.App"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             StartupUri="Window1.xaml"

             Startup="App_Startup"

             ShutdownMode="OnLastWindowClose">

</Application>

Listing 1

 

The code snippet in Listing 2 sets ShutdownMode at run-time in code-behind.

Application curApp = Application.Current;
curApp.ShutdownMode = ShutdownMode.OnLastWindowClose;

 

Listing 2