我尝试了以下代码:
p = StartProcess("some_process.exe");
DateTime startTime = DateTime.Now;
p.Start();
while (!p.HasExited)
{
executeTime = (DateTime.Now - startTime).Milliseconds;
if (executeTime > 10000) // 10 seconds
{
Console.Write("Test \"" + inputTest + "\" failed: takes more than 10 seconds");
break;
}
}
但它不起作用。
这有可能吗?
最佳答案
您可以使用 WaitForExit方法:
if (!p.WaitForExit((int)TimeSpan.FromSeconds(10).TotalMilliseconds))
{
// timeout exceeded while waiting for the process to exit
}
关于C# System.Diagnostics.Process : how to exit a process if it takes more than, 说,10 秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3708664/