可能是这个问题的反面:
Process.HasExited returns true even though process is running?
我在一个进程上调用了Kill(),它似乎已经退出了.但是当我测试HasExited时,我得到了错误:
myProcess.Kill(); while ( !myProcess.HasExited ) { Thread.Sleep(1000); }
这将持续无限期.当然,我必须更改此代码以最终停止等待,但我很好奇为什么HasExited仍然返回false,因为这个过程似乎从地图上掉了下来.
解决方法
你重定向标准输出?
MSDN声明如下:
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.
无论如何,建议的解决方法应该可以做到这一点:
myProcess.Kill(); myProcess.WaitForExit();