#1,100 – OnExplicitShutdown Shutdown Mode

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.

Below, we set ShutdownMode to OnExplicitShutdown.

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml"
             ShutdownMode="OnExplicitShutdown">
    <Application.Resources>

    </Application.Resources>
</Application>

If you run this application and then close its main window, you’ll see the application disappear from the “Apps” section of Task Manager, but still show up under “Background processes”.

1100-001

Below is sample code that explicitly shuts down the application 10 seconds after the main window closes.

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Shut things down 10 secs from now
            Timer t = new Timer(
                (state) => { App.Current.Shutdown(); }, 
                null, 10000, -1);
        }

 

Advertisement

#1,099 – OnMainWindowClose Shutdown Mode

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.

Below, we set ShutdownMode to OnMainWindowClose.

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml"
             ShutdownMode="OnMainWindowClose">
    <Application.Resources>

    </Application.Resources>
</Application>

After the application starts, we can create multiple child windows.

1099-001

At this point, if we close the main window, all three windows are closed and the application terminates.

#1,098 – OnLastWindowClose Shutdown Mode

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.

We can see this in action if we have a main window and then instantiate a second window on a Button click.

        // Button in main window, opens another
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OtherWindow ow = new OtherWindow();
            ow.Show();
        }

When we click on the button, the second window opens.

1098-001

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.

#55 – Application.ShutdownMode

The WPF Application class has a ShutdownMode property that indicates when an application should terminate, based on which windows are closed.

By default, the value of this property is ShutdownMode.OnLastWindowClose.  This indicates that the application will terminate after you’ve closed all of its windows.

You can also set the property to ShutdownMode.OnMainWindowClose.  When you do this, your application will terminate as soon as you close its main window, which will automatically close any other open windows that the application created.