tags:

views:

185

answers:

1

Possibly the inverse of this question: http://stackoverflow.com/questions/2519673/

I called Kill() on a process and it seems to have exited. But when I test HasExited, I get false:

myProcess.Kill();

while ( !myProcess.HasExited )
{
    Thread.Sleep(1000);
}

And this continues indefinitely. Granted, I have to change this code to stop waiting eventually, but I'm curious as to why HasExited still returns false when the process seems to have dropped off the map so to speak.

+3  A: 

Are you redirecting standard output? MSDN states the following:

When standard output has been redirected to asynchronous event handlers, it is possible that output processing will not have completed when this property returns true. To ensure that asynchronous event handling has been completed, call the WaitForExit() overload that takes no parameter before checking HasExited.

Anyway, the suggested workaround should possibly do the trick:

myProcess.Kill();
myProcess.WaitForExit();
0xA3
That's right, I did redirect standard output. I opted to put Kill and WaitForExit(1000) in a retry thrice loop though. I can't afford to wait forever as the MSDN article suggests.
Jeremy
I also wonder if this means that its possible for a process to terminate and still have its standard output streams open. Or does the article mean that my local streamreader hasn't gotten the message to close despite its source process exiting.
Jeremy