How to use Process.WaitForExit

user724198 picture user724198 · Jun 14, 2011 · Viewed 48.3k times · Source

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

Answer

Wicked Coder picture Wicked Coder · Jun 14, 2011

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