vb.net 如何使用 Process.WaitForExit
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6346651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Process.WaitForExit
提问by carlosfigueira
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?
我正在调用一个“有时”在 VB.NET 中工作的第三部分应用程序(它是一个自托管的 WCF)。但有时第 3 方应用程序会永远挂起,因此我为其添加了 90 秒计时器。问题是,我怎么知道事情是否超时?
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
杰森
回答by Wicked Coder
There have been known issues in the past where apps would freeze when using WaitForExit.
过去曾出现过使用 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
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx
回答by carlosfigueira
Check the method return value - http://msdn.microsoft.com/en-us/library/ty0d8k56.aspx- if the call timed out, it will return False.
检查方法返回值 - http://msdn.microsoft.com/en-us/library/ty0d8k56.aspx- 如果调用超时,它将返回 False。
回答by user2045900
if(process.WaitForExit(timeout)) {
// user exited
} else {
// timeout (perhaps process.Kill();)
}