Process.HasExited 可能抛出 InvalidOperationException 的原因是什么?

发布于 2024-08-28 08:32:11 字数 141 浏览 9 评论 0原文

我看到 System.Diagnostics.Process.HasExited 方法抛出 InvalidOperationException,但消息文本属性对于抛出它的原因并不是非常有用。什么情况下会抛出这个异常?

I'm seeing a System.Diagnostics.Process.HasExited method throw an InvalidOperationException, but the message text property is not terribly useful as to why it was thrown. Under what conditions does this exception get thrown?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

四叶草在未来唯美盛开 2024-09-04 08:32:11

我看到同样的消息。如果你这样做的话,就会发生这种情况:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "trash filename here.exe";
try
{
    proc.Start();
}
catch { }//proc should fail.
try
{
    if (proc.HasExited)
    {
        //....
    }
}
catch (System.InvalidOperationException e)
{
    //cry and weep about it here.
}

如果上面的 proc.Start() 失败了,你也应该哭泣和哭泣。因此,如果您在 proc.Start() 之后捕获,请务必在 proc.HasExited 处捕获(以及许多其他 System.Diagnostics.Process > 方法。

I'm seeing the same message. It can happen if you do this:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "trash filename here.exe";
try
{
    proc.Start();
}
catch { }//proc should fail.
try
{
    if (proc.HasExited)
    {
        //....
    }
}
catch (System.InvalidOperationException e)
{
    //cry and weep about it here.
}

If proc.Start() failed above, you should get to cry and weep section, too. So, if you catch after proc.Start() be sure to catch at proc.HasExited (and MANY other of the System.Diagnostics.Process Methods.

半岛未凉 2024-09-04 08:32:11

正如 Obalix 正确指出的那样,当没有进程附加到 Process 对象时,会抛出 InvalidOperationException 。当进程退出并且在 Process 对象上调用 CloseDispose 时,就会发生这种情况。 Close 从内存中释放与该进程相关的所有资源。在调用Close之前,这些数据被保存在内存中,以便为您(程序员)提供您想了解的有关已退出进程的信息,例如ExitTime退出代码

As Obalix correctly states, an InvalidOperationException is thrown when no process is attached to the Process object. This happens when a process has exited and Close or Dispose has been called on the Process object. Close releases all resources related to the process from memory. Before calling Close, this data was kept in memory to provide you (the programmer) with the information you want to know about the exited process, such as it's ExitTime and ExitCode.

清君侧 2024-09-04 08:32:11

文档指出 InvalidOperation 异常是在没有与该对象关联的进程中抛出。

您是否已经使用 Process.Start() 启动了该进程,或者在访问 HasExited 属性之前该进程已被释放?

这篇帖子也解决了同样的问题。

The documentation states that an InvalidOperation exception is thrown in no process is associated with the object.

Have you already started the process using Process.Start() or was the process disposed before you are accessing the HasExited property?

This post also deals whith the same issue.

心头的小情儿 2024-09-04 08:32:11

如果上述两个答案考虑到进程的实例成员不是线程安全的,那么这可能是下一个开始寻找的地方。

If the above two answers take in mind that process's instance members aren't thread safe, so that might be the next place to start looking.

白云不回头 2024-09-04 08:32:11

不要调用 Terminate.Close(),而是调用 Terminate.CloseMainWindoe()

然后,您可以发出定时等待,检查 HasExited 并根据需要调用 Kill()

Don't call Terminate.Close(), call Terminate.CloseMainWindoe() instead.

You may then issue a timed wait, check for HasExited and call Kill() if required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文