문제

I'm starting a new process using the following code:

Process p = new Process();
p.StartInfo.FileName = "...";
p.StartInfo.Arguments = "...";
p.Start();
p.WaitForExit(300000); // 5 minutes

if (!p.HasExited) 
    p.Kill();
Console.Write(p.ExitCode);

When the process ends within the 5 minutes, that's working, but when it doesn't, I get

InvalidOperationException (Process must exit before requested information can be determined...).

Any idea why I'm getting this exception?

Thank you.

도움이 되었습니까?

해결책

According to MSDN, "The Kill method executes asynchronously. After calling the Kill method, call the WaitForExit method to wait for the process to exit, or check the HasExited property to determine if the process has exited."

In other words, just because Kill returns doesn't mean the process is actually gone. You'll need to call WaitForExit to wait until the process has actually disappeared.

다른 팁

Some properties of a Process (such as HasExited) can be determined only after the process has quit. Hence the error.

I would suggest to have a try/catch block to get the exception happening.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top