【发布时间】:2012-03-05 20:05:52
【问题描述】:
我正在尝试启动一个外部进程并等待它处于活动状态,然后再继续执行我的程序。我正在根据进程名称进行搜索,但如果进程名称不是我所期望的,我的实现就会出现问题。
//When Method1 is called, it will load the data and bring a pop up
//as adobe save dialog box (as a save dialog exe in the task bar)
Method1();
while (true)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains("AdobeARM"))
{
isOpened = true;
}
}
//Once the pop up from Method 1 comes i call other methods
if (isOpened)
{
Method2();
Method3();
Method4();
break;
}
}
如果永远找不到进程,这可能会导致无限循环!处理此问题或替代while 循环的最佳方法是什么?
【问题讨论】:
-
设置某种限制,可以是尝试次数,也可以是放弃检查前的时间长度。如果你走那条路,你绝对应该在检查之间暂停(正如@M.Babcock 建议的那样。)
-
这是为了检查它是否正在运行?还是监控并等待它运行?