Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 进程是否实时运行_C# - Fatal编程技术网

C# 进程是否实时运行

C# 进程是否实时运行,c#,C#,是否可以实时跟踪流程? 例如,我有一个与游戏关联的应用程序。如果游戏正在运行,按钮将工作;如果游戏关闭,按钮将等待游戏开始。 如何实现这一点? 我发现了这个代码,但我的不是很好:) 您包含的代码段可以工作,但它非常占用资源,并且还会阻塞执行线程。您可以使用计时器事件检查进程状态 Timer myTimer = new Timer(); myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent); myTimer.Interval =

是否可以实时跟踪流程? 例如,我有一个与游戏关联的应用程序。如果游戏正在运行,按钮将工作;如果游戏关闭,按钮将等待游戏开始。 如何实现这一点? 我发现了这个代码,但我的不是很好:)


您包含的代码段可以工作,但它非常占用资源,并且还会阻塞执行线程。您可以使用计时器事件检查进程状态

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

}

创建父子关系。父进程有一个带有按钮的窗体,单击该按钮时将启动子进程。然后,父进程可以轻松跟踪子进程,等待它终止。当子进程终止时,重新启用该按钮。不,它不会以这种方式工作。我不需要开始这个过程。它可能已经由用户运行。太好了。。。谢谢你。
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

}