我正在尝试启动外部进程并在继续执行程序之前等待它处于活动状态。我正在根据进程名称进行搜索,但如果进程名称不符合我的预期,我的实现会出现问题。
//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
循环的替代方法或替代方法的最佳方法是什么?
答案 0 :(得分:1)
摆脱困境。
它将执行检查,然后如果AdobeARM进程在那里它将运行您的方法,如果没有将跳过它们。将Method1之后的所有内容放在单独的函数中,并从System.Threading.Timer调用该函数。
请注意,根据应用程序的使用方式,您可能需要为线程问题添加额外的处理。
private Timer myTimer;
private void DoSomething()
{
if (myTimer != null)
{
myTimer.Dispose();
myTimer = null;
}
Method1();
myTimer = new Timer(CheckForProcess, null, 100, 100);
}
private void CheckForProcess(object state)
{
bool isOpened = false;
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains("AdobeARM"))
{
isOpened = true;
break;
}
}
//Once the pop up from Method 1 comes i call other methods
if (isOpened)
{
myTimer.Dispose();
myTimer = null;
Method2();
Method3();
Method4();
}
}
答案 1 :(得分:1)
你说'我正在尝试启动外部进程并等待它继续运行',所以我想你使用:
Process prc = new Process(...);
prc.Start();
如果是的话,我会打电话给
prc.WaitForInputIdle();
然后继续。
来自MSDN
WaitForInputIdle - Causes the Process component to wait indefinitely for the associated process to enter an idle state. This overload applies only to processes with a user interface and, therefore, a message loop.
答案 2 :(得分:0)
可能只是休息一下?
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;
}
else
break; //BREAK HERE, CAUSE NO PROCESSES of ADOBE...
}
如果您想要一次又一次地提供此代码,请使用Timer
,但在这种情况下,显然不要使用更多while
循环。
答案 3 :(得分:0)
使用睡眠总是有帮助
System.Threading.Thread.Sleep(100); //100ms pause before continuing
有一个计数器或其他东西也可能有意义,假设如果你已经循环超过500次(延迟50秒)你将不会找到它并且应该突破循环
暂停将允许您的屏幕重新绘制,而不是完全锁定线程。
答案 4 :(得分:0)
而不是while(true),尝试一个基于某个超时值结束的while循环:
//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();
//define a "timeout" time one minute from now. (or whatever)
var timeout = DateTime.Now.AddSeconds(60);
//loop only while the current time has not exceeded the timeout
while (DateTime.Now < timeout)
{
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;
}
else
Thread.Yield(); //gives other threads CPU time; Thread.Sleep() works too.
}
//If we exit the loop without isOpened being set, we should probably tell someone.
if(!isOpened) throw new Exception("The operation timed out waiting for AdobeARM.");