C#:即使进程已终止,Process.HasExited 也会返回 false

标签 c# process

可能是这个问题的反面:Process.HasExited returns true even though process is running?

我在进程上调用了 Kill(),它似乎已经退出。但是当我测试 HasExited 时,我得到了 false:

myProcess.Kill();

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

而且这种情况会无限期地持续下去。当然,我必须更改此代码以最终停止等待,但我很好奇为什么 HasExited 仍然返回 false,可以这么说,当进程似乎已经从 map 上掉下来时。

最佳答案

你在重定向标准输出吗? 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();

关于C#:即使进程已终止,Process.HasExited 也会返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2779653/

相关文章:

c# - 该命令需要至少两行源数据

dll - 我们可以在一个进程中加载​​ 2 个同名的 DLL

C# 在内存中编译并创建内存组件类型的实例

c# - 如何在jquery中制作ajax过滤器?

ruby-on-rails - Ruby 使用不同的用户运行 bash 命令

linux - 如何检查进程是否有正在运行的磁盘 I/O 请求?

c++ - 使用线程检查c++中是否存在进程

Java - 从同一个方法/类打开程序、网站或文件夹

c# - Silverlight -- Debug.WriteLine() 不适用于长字符串

c# - '调用与指定绑定(bind)约束匹配的类型的构造函数引发异常