How to use Process.WaitForExit

纵饮孤独 提交于 2019-12-21 06:21:08

问题


I'm calling a 3rd part app which 'sometimes' works in VB.NET (it's a self-hosted WCF). But sometimes the 3rd party app will hang forever, so I've added a 90-second timer to it. Problem is, how do I know if the thing timed out?

Code looks like this:

Dim MyProcess as System.Diagnostics.Process = System.Diagnostics.Process.Start(MyInfo)
MyProcess.WaitForExit(90000)

What I'd like to do is something like this

If MyProcess.ExceededTimeout Then
    MyFunction = False
Else
    MyFunction = True
End If

Any ideas?

Thanks,

Jason


回答1:


Check the method return value - http://msdn.microsoft.com/en-us/library/ty0d8k56.aspx - if the call timed out, it will return False.




回答2:


There have been known issues in the past where apps would freeze when using WaitForExit.

You need to use

dim Output as String = MyProcess.StandardOutput.ReadToEnd()

before calling

MyProcess.WaitForExit(90000)

Refer to Microsoft's snippet:

// Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx




回答3:


if(process.WaitForExit(timeout)) {
    // user exited
} else {
    // timeout (perhaps process.Kill();)
}

Async process start and wait for it to finish



来源:https://stackoverflow.com/questions/6346651/how-to-use-process-waitforexit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!