是否可以实时跟踪的过程吗?例如,我有一个与游戏相关的应用程序。如果游戏运行时,该按钮的作品,如果游戏被关闭,为游戏的按钮等待启动。这又如何实现?我发现这个代码,但我不是很好:)
while (true)
{
System.Diagnostics.Process[] procs =
System.Diagnostics.Process.GetProcessesByName("notepad");
if (procs.Count() == 0)
break;
}
该代码块已包含的作品,但它是非常耗费资源,而且它还可以拦截执行的线程。您可以使用计时器事件来检查进程状态。
Timer myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
myTimer.Interval = 1000; // 1000 ms is one second
myTimer.Start();
public static void DisplayTimeEvent(object source, ElapsedEventArgs e)
{
System.Diagnostics.Process[] procs =
System.Diagnostics.Process.GetProcessesByName("notepad");
//isButtonEnabled is needed to be defined on the upper context
isButtonEnabled = procs.Count() != 0
}