What is the preferred method of programmatically closing a C# WPF Application?

TK. picture TK. · Jun 7, 2011 · Viewed 7.7k times · Source

Which is the preferred method of closing a C# WPF Application?

1) calling Window.Close() within the Main Window

2) Calling Application.Current.Shutdown( 0 ) within the Main Window.

Are the two semantically equivalent or is there a subtle distinction that I need to be aware of?

Answer

H.B. picture H.B. · Jun 7, 2011

The first only closes the window, depending on your Application.ShutdownMode that may not even shutdown the application.

How you exit the application depends solely on what you want to do, i for one tend to define the Application.MainWindow and set the ShutdownMode to OnMainWindowClose so all the small dialogues do not prevent the application from closing (since the default is OnLastWindowClose).