BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Start-up and Shutdown

The final part of the Windows Presentation Foundation Fundamentals tutorial looks at the start-up and shutdown options for WPF applications, including changing the way that a program launches and exits and detecting key events.

Shutdown

By default, a WPF application exits completely when all of its windows are closed. This is not suitable for a background application that minimises to the system tray, or for programs that cannot operate when their main window is closed. Handily, WPF provides several shutdown modes to allow greater control.

ShutdownMode Property

The ShutdownMode property allows you to specify what will trigger an application to exit. The property is set to a value from the ShutdownMode enumeration. The default mode is OnLastWindowClose. As the name suggests, once the user closes all of the windows, the program exits.

A second mode is provided by the OnMainWindowClose value. When an application's first window is instantiated, it is designated as the main window. If you set this shutdown mode, when the main window closes, the application exits. Any other open windows close automatically.

NB: A reference to the main window is held in the current Application object's MainWindow property. You can change this property to alter the main window.

Let's try it. Update the App.xaml to set the mode:

<Application x:Class="StartupShutdownDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup"
             ShutdownMode="OnMainWindowClose">
    <Application.Resources>

    </Application.Resources>
</Application>

As we have two windows, we need to be able to see which one of them is the main window. Update the code that runs on start-up to set the title of the main window:

private void Application_Startup(object sender, StartupEventArgs e)
{
    new MainWindow().Show();
    new MainWindow().Show();

    Application.Current.MainWindow.Title = "The MAIN Window";
}

Run the program to show the two windows. If you close the window with the title, "The MAIN Window", the application exits. However, closing the other window first leaves the main window open.

The third shutdown mode is OnExplicitShutdown. With this mode set, closing windows no longer automatically exits the application. To end the program, you must call the Shutdown method of the Application class.

To demonstrate, first set the mode in the App.xaml, as shown below:

<Application x:Class="StartupShutdownDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup"
             ShutdownMode="OnExplicitShutdown">
    <Application.Resources>

    </Application.Resources>
</Application>

Run the program and close the two windows. You will find that the application is still running, so Visual Studio's debugger remains active. Stop the debugger to return to editing.

Let's exit the application when its main window closes using the Shutdown method. Update the Application_Startup method to attach code to the main window's Closed event. When this event is captured, we'll show a message box and then exit.

private void Application_Startup(object sender, StartupEventArgs e)
{
    new MainWindow().Show();
    new MainWindow().Show();

    Application.Current.MainWindow.Title = "The MAIN Window";

    Application.Current.MainWindow.Closed += (s,a) =>
    {
        MessageBox.Show("Shutting Down", "Shutdown",
                        MessageBoxButton.OK, MessageBoxImage.Information);
        Shutdown();
    };
}

Run the program to see the results. When you close the main window, the message box will be shown. When you dismiss the message box, the application closes down.

Exit Event

The final Application member that we'll consider in this article is the Exit event. When you are using a shutdown mode that requires manual exit, you have full control over the shutdown process. For the other modes, you might want to detect when the program is about to exit so that you can perform any clean-up procedures. You can do this by capturing the Exit event.

The Exit event is raised just before the program closes. It occurs after you call Shutdown or after closing a window starts the exit process. The event is also raised if the user is logging off or shutting down Microsoft Windows. Once raised, it is not possible to cancel the process.

Let's attach the event in the App.xaml:

<Application x:Class="StartupShutdownDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Startup="Application_Startup"
             Exit="Application_Exit"
             ShutdownMode="OnExplicitShutdown">
    <Application.Resources>

    </Application.Resources>
</Application>

We'll simply show a message box when the event is detected:

private void Application_Exit(object sender, ExitEventArgs e)
{
    MessageBox.Show("Exit Event Raised", "Exit");
}

Run the program, then close the main window. You will now see two message boxes. Note the order in which they appear. You can see that the Exit event occurs after calling the Shutdown method.

18 July 2015