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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Apr 16, 2012, 12:25 AM EDT
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.
Muhammad Irshad53 reputationPosted Apr 16, 2012, 2:45 AM EDT
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
VulpesRank #55 · 29.2K reputationPosted Apr 16, 2012, 3:37 AM EDT
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.
Sam HobbsRank #107 · 20.5K reputationPosted Apr 16, 2012, 11:43 AM EDT
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.