Right way to close WPF GUI application: GetCurrentProcess().Kill(), Environment.Exit(0) or this.Shutdown()
Asked Answered
S

4

51

My GUI desktop-based WPF 4.0 (C# .Net 4.0) program works with SQL Server database. Each time when I run my application it creates a connection to SQL Server via ADO.NET Entity Framework and if SQL Server is not reachable it throws an exception and shows MessageBox with notification.

Now I want that after user read this message application will shut down. I found three ways to do this:

Process.GetCurrentProcess().Kill();

or

this.Shutdown(); // Application.Current.Shutdown()

or

System.Environment.Exit(0);

All of them work fine and do what I need — close application and kill application's process in Windows Task Manager.

My questions:

  1. What is the difference between them?

  2. Which way will close my application faster?

  3. Which way to close application should I use?

  4. Is Application.Current.Shutdown() and this.Shutdown() the same way to close application? Or maybe there is another, more suitable, way to close a WPF GUI application?

Application.Exit() doesn't work for me as I get the error:

The event 'System.Windows.Application.Exit' can only appear on the left-hand side of += or -=

Saltarello answered 7/10, 2010 at 10:44 Comment(0)
P
70

Application.Current.Shutdown() is the proper way to shutdown an application. Generally, because it fires the exit events, that you can handle, further info.

Process.GetCurrentProcess().Kill() should be used when you want to kill the application as soon as possible, further info.

The nature of those methods is totally different:

  • Shutdown() can be paused to end some operations
  • Kill() forces the application to close immediately, as soon as possible

Probably, Kill() will be the fastest way, but this is something like kernel panic.

Pater answered 7/10, 2010 at 11:14 Comment(2)
>> Ad4. That depend what this is. I call connection function in App.cs constructor, so «this» — it's instance of App-class.Saltarello
I use Process.GetCurrentProcess().Kill() when the app can't find a data context (where everything is saved), and the user declines to tell the app where it is. Since nothing can be saved anyway, the app just self-immolates.Circuit
P
2

Use Application.Current.Shutdown();

Add ShutdownMode="OnMainWindowClose" in App.xaml

Propel answered 13/12, 2015 at 17:22 Comment(1)
For shutdown mode info: msdn.microsoft.com/en-us/library/…Littlejohn
C
1
private void ExitMenu_Click(object sender, RoutedEventArgs e)
    {
        Application.Current.Shutdown();
    }
Cupping answered 23/5, 2016 at 16:7 Comment(1)
you don't need to add anything to the App.xamlCupping
C
1

@Damian Leszczyński - Vash's answer pretty much covers the 4 specific questions you asked. For your final question on Application.Exit(), that's an event you can subscribe to, not a method that you can call. It should be used like this:

Application.Current.Exit += CurrentOnExit; 
//this.Exit += CurrentOnExit; would also work if you're in your main application class

...

private void CurrentOnExit(object sender, ExitEventArgs exitEventArgs)
{
    //do some final stuff here before the app shuts down
}
Cu answered 18/10, 2016 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.