开发者

How to use Process.WaitForExit

开发者 https://www.devze.com 2023-03-13 03:25 出处:网络
I\'m calling a 3rd part app which \'sometimes\' works in VB.NET (it\'s a self-hosted开发者_StackOverflow WCF). But sometimes the 3rd party app will hang forever, so I\'ve added a 90-second timer to it

I'm calling a 3rd part app which 'sometimes' works in VB.NET (it's a self-hosted开发者_StackOverflow 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


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


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


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

Async process start and wait for it to finish

0

精彩评论

暂无评论...
验证码 换一张
取 消