tags:

views:

70

answers:

1

Based on other questions, I'm using System.Diagnostics.Process to Start and Kill a process:

this.childProcess.Kill();
this.childProcess.WaitForExit();
this.childProcess.Close();

I would assume that WaitForExit deals with the asynchronous nature of the Kill command, however the process in question (perl.exe) refuses to die. Instead it lingers for some period of time.

This "period of time" is causing a race condition. Is there anyway I can make sure this process actually dies?

+3  A: 

I'm not sure what you mean by "make sure this process actually dies." Under the hood the Kill method eventually calls TerminateProcess which is the most effective way to kill a process and WaitForExit won't return until the process is actually gone.

The Kill method can fail to actually kill the process in at least 2 circumstances

  1. You don't have sufficient rights to kill the process
  2. There is a debugger attached to the process (I believe this prevents TerminateProcess from working).
  3. Uninterruptable I/O operation (thanks Daniel)

Do either of these apply to your scenario?

JaredPar
I believe I has sufficient rights since: a) I spawned it, b) it does "sometimes" die. I experience the problem if I have the debugger running on the Parent process, or if I don't us the debugger at all.
tzenes
There's another case where a process isn't able to die (at least, not for a while): if it's doing an uninterruptible I/O operation. It's highly likely that the perl.exe process in question is just doing some kind of file or socket I/O, and even after you call `TerminateProcess`, the OS will still wait for outstanding I/O to flush. If you have buggy drivers in the mix, this might not ever happen. Mark Russinovich had [a blog article covering this](http://blogs.technet.com/b/markrussinovich/archive/2005/08/17/unkillable-processes.aspx) a while back, which might be useful.
Daniel Pryden
@Daniel If it is doing some sort of uninterrupted I/O (say writing to disk) how do I know when it has actually finished?
tzenes