#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"


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


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


About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #1,099 – OnMainWindowClose Shutdown Mode

  1. Pingback: Dew Drop – June 23, 2014 (#1801) | Morning Dew

  2. Imran says:

    Note that the MainWindow property of the main Application object will refer to the instance of the window that created first time

  3. Imran says:


    private void Application_Startup(object sender, StartupEventArgs e)
    Window1 wnd = new Window1();
    MainWindow wnd = new MainWindow();

    // Now main wondow is window1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: