我看到一个System.Diagnostics.Process.HasExited
方法抛出了一个InvalidOperationException
,但是它的消息文本属性并没有提供有用的信息来解释为什么会抛出异常。在什么条件下会抛出这个异常呢?
我看到一个System.Diagnostics.Process.HasExited
方法抛出了一个InvalidOperationException
,但是它的消息文本属性并没有提供有用的信息来解释为什么会抛出异常。在什么条件下会抛出这个异常呢?
我看到了同样的信息。如果你做了以下操作,可能会出现这种情况:
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
方法)处进行捕获。Terminate.Close()
,改为调用 Terminate.CloseMainWindow()
。HasExited
并在需要时调用 Kill()
。