Wednesday, December 17, 2008

The Way of Quitting an Application

There are several ways of letting an application to quit programmatically. But you should choose the best one among them.

Environment.Exit Method

This method will force a process to terminate. So the process has no chance to do cleaning up work. No code after Environment.Exit will be executed. Any data that is unsaved will be lost. Not recommend this way.

Process.Kill Method

Killing a process just like Environment.Exit, terminates the process immediately and the process doesn't have the last chance to breath. So again, it's not recommended.

Process.CloseMainWindow Method

This method will send a request to a process' message loop, if the process does have a message loop. The process thus has a chance to do cleaning up work, and save unsaved data to disk then terminates gracefully. This method can be called either from another process or within the process that to be terminated.

Environment.Exit Method http://msdn.microsoft.com/en-us/library/system.environment.exit.aspx

Process.CloseMainWindow Method http://msdn.microsoft.com/en-us/library/system.diagnostics.process.closemainwindow.aspx