gpt4 book ai didi

vb.net - 如何使用 Process.WaitForExit

转载 作者:行者123 更新时间:2023-12-02 06:22:52 25 4
gpt4 key购买 nike

我正在调用一个“有时”在 VB.NET 中运行的第三部分应用程序(它是一个自托管的 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

有什么想法吗?

谢谢,

杰森

最佳答案

过去存在一些已知问题,即使用 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

关于vb.net - 如何使用 Process.WaitForExit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6346651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com