WPF App Doesn't Shut Down When Closing Main Window
In your MainWindow.xaml.cs
, try doing this:
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
Application.Current.Shutdown();
}
Per this link, you can also set the ShutdownMode
in XAML:
http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
ShutdownMode="OnExplicitShutdown"
>
</Application>
Applications stop running only when the Shutdown
method of the Application
is called. Shut down can occur implicitly or explicitly, as specified by the value of the ShutdownMode
property.
If you set ShutdownMode
to OnLastWindowClose
, Windows Presentation Foundation (WPF) implicitly calls Shutdown when the last window in an application closes, even if any currently instantiated windows are set as the main window (see MainWindow).
A ShutdownMode
of OnMainWindowClose
causes WPF to implicitly call Shutdown when the MainWindow closes, even if other windows are currently open.
The lifetime of some applications may not be dependent on when the main window or last window is closed, or may not be dependent on windows at all. For these scenarios you need to set the ShutdownMode
property to OnExplicitShutdown
, which requires an explicit Shutdown
method call to stop the application. Otherwise, the application continues running in the background.
ShutdownMode
can be configured declaratively from XAML or programmatically from code.
This property is available only from the thread that created the Application
object.
In your case, the app isn't closing because you're probably using the default OnLastWindowClose
:
If you set ShutdownMode
to OnLastWindowClose
, WPF implicitly calls Shutdown when the last window in an application closes, even if any currently instantiated windows are set as the main window (see MainWindow
).
Since you're opening a new window, and not closing it, shutdown doesn't get called.
I'm glad you got your answer but for the sake of others I'll answer your question as-well to add some information.
Step 1
First, if you want your program to exit when the main window closes down, you need to specify, since this is not WinForms where this behavior is default.
(The default in WPF is when the last window closes down)
In Code
Go to your application instance in your entry point (In VS 2012's WPF program the default is nested inside App.xaml
, so go inside it and navigate to App.xaml.cs
& create a constructor).
In the constructor specify that your Application
's ShutdownMode
should be ShutdownMode
.OnLastWindowClose
.
public App()
{
ShutdownMode = ShutdownMode.OnLastWindowClose;
}
In XAML
Go to your App.xaml
file that VS 2012 created by default (or create it yourself)
The root is an Application
, specify inside that your Application
's ShutdownMode
should be ShutdownMode
.OnLastWindowClose
.
<Application x:Class="WpfApplication27.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
ShutdownMode="OnMainWindowClose">
If it works, you're done; you can stop reading.
Step 2
If the above didn't work (I guess you wrote the WPF application from scratch), the main window probably isn't known to the application as the main window. So specify that as-well.
In Code
Go to the application's constructor as you did in Step 1, and specify that Application
.MainWindow
's value is your Window
:
MainWindow = mainWindow;
In XAML
Go to the Application
XAML as you did in Step 1, and specify that Application
.MainWindow
's value is your Window
:
MainWindow = "mainWindow";
Alternative
I don't think this is the best approach, just because WPF doesn't want you to do this (so it has Application
's ShutdownMode
), but you can just use an event / override an event method (OnEventHappened).
Go to the MainWindow's code-behind file and add:
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
App.Current.Shutdown();
}
Because the default shutdown mode in a WPF application is OnLastWindowClose, which means the application stops when the last window closes.
When you instantiate a new Window object, it automatically gets added to the list of windows in the application. So, the problem was that your application was creating two windows when it started - the MainWindow and the not-yet-shown Window01 - and if you only closed the MainWindow, the Window01 would keep your application running.
Normally, you will create a window object in the same method that is going to call its ShowDialog, and you will create a new window object each time the dialog is shown.