我有一个轮询计时器来检查进程是否正在运行。我有以下简单的代码:
bool alreadyChecked = false; //check if the wait to check the second time is already over
**Timer_elapsed event**
Process sampleProcess[] = Process.GetProcessesByName("notepad");
if(sampleProcess.length > 0)
{
//Process is running
return;
}
else
{
//Process is not running, so do the following
//Wait for some time and check again (set alreadyChecked = true when the wait is over)
if (alreadyChecked){
//Run the process}
else{
//The process has started running while we were waiting
return;}
}
我无法waiting code
在事件内部实现,因此它可以等待然后再次触发事件。(即使我们实现了等待时间,Timer_elapsed 事件也会在我们等待时再次被定时器触发。)
有什么建议么?