Hi Guys,
In Process.GetCurrentProcess.Kill() vs. End
What is better to user and why ?
Please, guide about these ?
Regards,
Itz.Irshad
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Apr 16, 2012, 3:43 PM
The best solution for desktop applications is to close all windows (and threads if relevant) so that the message loop (all message loops if relevant, but it is nearly certain there is only one for your application) so that your application finishes normally. If you cannot do that then there is likely a problem with the design. If that problem exists, then you should analyze why. If you are not able to fix the design then you should at least understand the problem well enough so you do not repeat the problem in other applications.
You should read the relevant documentation. Killing a process is a very bad thing. As Vulpes says, do not do that unless abslutley necesary. Do not design an application to use Kill as a normal way to exit; not even for errors unless there is absolutely no other choice. You should not put a call to Kill in your applicaiton; let the user use it if needed but you should do everything you can to avoid the need for it.
As Vulpes said, the End statement uses Environment.Exit(). The problem with functions such as that is that they cause the message loop to exit immediately and the application does not have the opportunity to do cleanup that it would normally do. You can read the documentation of the End statement to understand it is also not good to use without the cleanup that is described in that documentation page.
VulpesPosted Apr 16, 2012, 7:37 AM
End calls Environment.Exit() which calls the API function _exit() which is specifically designed to kill the current process dead.
Process.GetCurrentProcess().Kill() calls the API function TerminateProcess() which is designed to kill any process and so is less direct than _exit().
End should work with ASP.NET applications as well as desktop applications but will give you a browser error.
Muhammad IrshadPosted Apr 16, 2012, 6:45 AM
The scenario is here. I want to close my application if an error occurs.
So, what would be best choice between them in case of:
- Desktop Application
- Web Application
And Thanks for noticing. Its VB.NET's End Statement.
Thanks
Itz Irshad
VulpesPosted Apr 16, 2012, 4:25 AM
You should allow them to end gracefully where possible.
If you do need to end a Windows Forms application prematurely and you can't close the main form directly for some reason, then it's better to use Application.Exit, which does some cleaning up, rather than Process.GetCurrentProcess.Kill or its equivalent Environment.Exit(1) which cause an abrupt termination. The latter two can be used to force other types of application to end.
When you say 'End', I assume you must mean VB.NET's End statement:
http://msdn.microsoft.com/en-us/library/0wt87xba.aspx
This also causes an abrupt termination and calls Environment.Exit(1) under the hood.