Tutorial :Why shouldn't I use Process.GetCurrentProcess().Kill() to exit my WinForm application?


Right now, when the user want to exit my application, I do the few things I have to (ie disconnecting from the server, saving the user data...) and then I do the following :

  • Exit all my mainloops using booleans
  • abort the threads still running (typically, my server polling thread)
  • kindly call Application.Exit();

This takes a few seconds to exit, and serves no real purpose (everything is already saved on the server, so I don't really care what happens there)

If I use this instead, I got instant termination with no drawback I can think of :


Why wouldn't I just terminate my process and let the CLR drop the AppDomain ?

I know that carefully disposing your shared resources (IO file handlers, etc.) is important (so please don't answer that:)), but once it's done, is there a real reason to cleanly exit my App ?


Killing the process would mean that finally blocks will not be executed, and probably not even critical finalizer objects, which may actually be critical, and cause resource leakage at system level. It can also cause unexpected bugs in the future when someone else maintains the code, as they (or you) will have to twist their heads, having to think every time they write a finally block whether it will be executed.


That's indeed a good question!

Once upon a time (over ten years ago) I wrote a VB6 app wich hung at termination due to a confirmed bug in the WinINet OCX (or whatever it was called) due to the particular certificate used on a web server it was talking to.

Without any way to come around this bug I used TerminateProcess and for all that I know, this app was in use on several thousand machines for several years and I never heard of any problem regarding this hack!


Firstly, I think you mean Process.Kill(), not Process.TerminateProcess(). Secondly, killing the process will just rip it out. Any cleanup code will not execute and your application will not get a chance to cancel the kill (eg. if the user has unsaved data). If you're happy with that, go for it.


Keep in mind that if you are writing to a file/network stream it's possible that your write will be imcomplete.

If your application is ready to deal with corrupt data in that way, then there's no reason not to do it. However, if you can exit by other means I'd recommend that.


It should be noted that finalizers may actually be never executed, so killing the process is not much different from any other reason to skip finalization code, like slow finalizers or a finalizer throwing an exception.

All in all, I wouldn't worry much about closing the application taking a few seconds, especially if it immediately hides all its windows and "disappears" for the user. However, if closing takes dozens of seconds and/or the user is likely to execute the application again and it doesn't support multiple instances, calling Process.Kill may be a good idea.

I know some applications which take ages to terminate, and I hope to see them one day just brutally killing themselves rather than making me, the user, do it (it's especially annoying on OS restart).


Using Third party libraries with no way to catch their errors or leaks, sometimes the only way to end it without throwing up a crash MessageBox, is to Kill the process.


Why not use System.Environment.Exit(int)? It seems like you want to do this just to save code. To me it's much clearer to see the call to Exit does and your shutdown will be more controlled, Finalizers and Critical Finalizers will run.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »