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

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


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).


Definitely Window.Close(); This will give all other windows to properly shut down and finalize their disposable resources. Note that you should call this on main window to close the entire app (i.e. this.Close(); from the main window).


Application.Shutdown closes all open windows and disposes them, also you have the possibility to return another exit code. So I'd (usually) go with Application.Shutdown.