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?
5 回答
我看到了同样的消息。如果你这样做,它可能会发生:
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
方法。
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
.
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.
如果上述两个答案考虑到进程的实例成员不是线程安全的,那么这可能是下一个开始寻找的地方。
不要打电话Terminate.Close()
,Terminate.CloseMainWindoe()
而是打电话。
然后,您可以发出定时等待、检查并在需要时HasExited
致电。Kill()