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?

Community
  • 1
  • 1
tzenes
  • 1,699
  • 2
  • 15
  • 31
  • This is an XY question. Y is "why can't I kill the process". X is "why did I write code to kill the process in the first place". I have to recommend pursuing X. – Hans Passant Sep 20 '10 at 23:31
  • @Hans I think knowing why a process isn't dying is a useful step in analyzing the data flow in my project. I could rewrite large sections of my project on the assumption that the process can't be killed only to reintroduce the same problem. – tzenes Sep 20 '10 at 23:35

1 Answers1

3

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
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 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 Sep 20 '10 at 22:35
  • 1
    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 Sep 20 '10 at 22:45
  • 1
    @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 Sep 20 '10 at 22:59