2

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

3 Answers3

5

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

Wicked Coder
  • 1,128
  • 6
  • 8
  • Hangs like these can occur depending on the environment. I have a built-in process runner that may execute in a web, console or windows service environment. In my case only the console environment completed successfully (and fired the Exited event). Once I started reading the stream (both synchronously as asynchronously) the process exited as expected in all three environments. – Robert Sirre Oct 14 '14 at 09:27
4

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

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
2
if(process.WaitForExit(timeout)) {
    // user exited
} else {
    // timeout (perhaps process.Kill();)
}

Async process start and wait for it to finish

Community
  • 1
  • 1