Pages

Tuesday, 18 March 2014

How to Close or Exit From a WPF Application Programmatically Correctly


I recently figured out that a WPF Application cannot close as we suppose to do in windows applications. So I Googled it, I have found several ways to do it and I am in a mood to share those with you guys.

The best and proper way to close WPF application is by calling Shutdown() method;

Calling it may give you several advantages, First is that method will call "Exit" event. Using "Exit" event you can handle the necessary processing that suppose to be done right before the application stop running. You just do not have to worry about the windows you opened and hide before this method is called. All those windows will be closed automatically.

And also this method would call implicitly if you have set the "ShutdownMode" to "OnLastWindowClose", "OnMainWindowClose" or by ending the session.

This has support from framework 3.0 and above.

Anyway do not use following to close your application;
 
Because it will kill your application right that moment and will not call the "Exit" event. I think that is not a good idea.

No comments:

Post a Comment