问题
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