Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 341
TK. Avatar asked Jun 07 '11 13:06

TK.


2 Answers

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

like image 114
H.B. Avatar answered Nov 14 '22 08:11

H.B.


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

like image 32
Teoman Soygul Avatar answered Nov 14 '22 06:11

Teoman Soygul