Process.HasExited 可能抛出 InvalidOperationException 的原因是什么?
我看到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我看到同样的消息。如果你这样做的话,就会发生这种情况:
如果上面的
proc.Start()
失败了,你也应该哭泣和哭泣。因此,如果您在proc.Start()
之后捕获,请务必在proc.HasExited
处捕获(以及许多其他System.Diagnostics.Process
> 方法。I'm seeing the same message. It can happen if you do this:
If
proc.Start()
failed above, you should get to cry and weep section, too. So, if you catch afterproc.Start()
be sure to catch atproc.HasExited
(and MANY other of theSystem.Diagnostics.Process
Methods.正如 Obalix 正确指出的那样,当没有进程附加到
Process
对象时,会抛出InvalidOperationException
。当进程退出并且在Process
对象上调用Close
或Dispose
时,就会发生这种情况。Close
从内存中释放与该进程相关的所有资源。在调用Close
之前,这些数据被保存在内存中,以便为您(程序员)提供您想了解的有关已退出进程的信息,例如ExitTime
和退出代码
。As Obalix correctly states, an
InvalidOperationException
is thrown when no process is attached to theProcess
object. This happens when a process has exited andClose
orDispose
has been called on theProcess
object.Close
releases all resources related to the process from memory. Before callingClose
, 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'sExitTime
andExitCode
.文档指出 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 theHasExited
property?This post also deals whith the same issue.
如果上述两个答案考虑到进程的实例成员不是线程安全的,那么这可能是下一个开始寻找的地方。
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.
不要调用
Terminate.Close()
,而是调用Terminate.CloseMainWindoe()
。然后,您可以发出定时等待,检查
HasExited
并根据需要调用Kill()
。Don't call
Terminate.Close()
, callTerminate.CloseMainWindoe()
instead.You may then issue a timed wait, check for
HasExited
and callKill()
if required.