Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exit a WPF application programmatically?

People also ask

How do I close a programmatically program in WPF?

Shutdown method of Application class in WPF is used to stop an application immediately regardless if Close event is fired or not. Shutdown method is called implicitly on Window close and MainWindow close events when Shutdown mode is set to OnLastWindowClose, OnMainWindowClose respectively.

How do I close WPF?

An Exit item in the File menu, typically for main application windows. A Close item in the File menu, typically on a secondary application window. A Cancel button, typically on a modal dialog box. A Close button, typically on a modeless dialog box.

How do I close an application in C#?

Exit a Console Application With the Application. Exit() Function in C# The Application. Exit() function terminates all the message loops started with the Application.

How do I close XAML Windows?

Use the this. Close() method in the MainWindow. xaml. cs file to close the window.


To exit your application you can call

System.Windows.Application.Current.Shutdown();

As described in the documentation to the Application.Shutdown method you can also modify the shutdown behavior of your application by specifying a ShutdownMode:

Shutdown is implicitly called by Windows Presentation Foundation (WPF) in the following situations:

  • When ShutdownMode is set to OnLastWindowClose.
  • When the ShutdownMode is set to OnMainWindowClose.
  • When a user ends a session and the SessionEnding event is either unhandled, or handled without cancellation.

Please also note that Application.Current.Shutdown(); may only be called from the thread that created the Application object, i.e. normally the main thread.


If you really need it to close out you can also use Environment.Exit(), but it is not graceful at all (more like ending the process).

Use it as follows:

Environment.Exit(0)

As wuminqi said, Application.Current.Shutdown(); is irreversible, and I believe it is typically used to force an application to close at times such as when a user is logging off or shutting down Windows.

Instead, call this.close() in your main window. This is the same as pressing Alt + F4 or the close [x] button on the window. This will cause all other owned windows to close and will end up calling Application.Current.Shutdown(); so long as the close action wasn't cancelled. Please see the MSDN documentation on Closing a Window.

Also, because this.close() is cancellable you can put in a save changes confirmation dialog in the closing event handler. Simply make an event handler for <Window Closing="..."> and change e.Cancel accordingly. (See the MSDN documentation for more details on how to do this.)


Use any of the following as needed:

1.

 App.Current.Shutdown();
OR
 Application.Current.Shutdown();

2.

 App.Current.MainWindow.Close();
OR
 Application.Current.MainWindow.Close();

Above all methods will call closing event of Window class and execution may stop at some point (cause usually applications put dialogues like 'are you sure?' or 'Would you like to save data before closing?', before a window is closed completely)

3. But if you want to terminate the application without any warning immediately. Use below

   Environment.Exit(0);

This should do the trick:

Application.Current.Shutdown();

If you're interested, here's some additional material that I found helpful:

Details on Application.Current

WPF Application LifeCycle


Here's how I do mine:

// Any control that causes the Window.Closing even to trigger.
private void MenuItemExit_Click(object sender, RoutedEventArgs e)
{
    this.Close();
}

// Method to handle the Window.Closing event.
private void Window_Closing(object sender, CancelEventArgs e)
{
    var response = MessageBox.Show("Do you really want to exit?", "Exiting...",
                                   MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
    if (response == MessageBoxResult.No)
    {
        e.Cancel = true;
    }
    else
    {
        Application.Current.Shutdown();
    }
}

I only call for Application.Current.ShutDown() from the main application window, all other windows use this.Close(). In my main window, Window_Closing(...) handles the top right x button. If any of the methods call for window closer, Window_Closing(...) grabs the event for shut down if user confirms.

The reason I do in fact use Application.Current.Shutdown() in my main window is that I've noticed that if a design mistake was made and I haven't declared a parent of one of my windows in an application, if that window is opened without being shown prior to the last active window closing, I'm left with a hidden window running in the background. The application will not shut down. The only way to prevent complete memory leak is for me to go into the Task Manager to shut down the application. Application.Current.Shutdown() protects me from unintended design flaws.

That is from my personal experience. In the end, use what is best for your scenario. This is just another piece of information.


There should not be an Application.ShutDown(); or .Exit() message.

Application is a static class. It does not refer to the current application. You need to get to the current application and then shut it down like this:

Application.Current.Shutdown();