2

我正在调用一个“有时”在 VB.NET 中工作的第 3 部分应用程序(它是一个自托管的 WCF)。但有时第 3 方应用程序会永远挂起,所以我为它添加了一个 90 秒计时器。问题是,我怎么知道事情是否超时?

代码如下所示:

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

我想做的是这样的

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

有任何想法吗?

谢谢,

杰森

4

3 回答 3

5

过去存在已知问题,即在使用 WaitForExit 时应用程序会冻结。

你需要使用

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

打电话之前

MyProcess.WaitForExit(90000)

参考微软的片段:

// 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

于 2011-06-14T16:22:13.093 回答
4

检查方法返回值 - http://msdn.microsoft.com/en-us/library/ty0d8k56.aspx - 如果调用超时,它将返回 False。

于 2011-06-14T16:16:29.580 回答
2
if(process.WaitForExit(timeout)) {
    // user exited
} else {
    // timeout (perhaps process.Kill();)
}

异步进程启动并等待它完成

于 2013-02-06T07:32:52.013 回答