C#等待程序/进程启动

时间:2013-06-21 02:22:30

标签: c# process startup wait

如何在程序/流程声明打开的情况下创建一个程序来激活代码而不启动所声明的程序本身。

现在我有了这段代码:

while (!IsProcessOpen("ThisProcess")) //"IsProcessOpen" just checks whether the program stated is open in the task manager.
{

}
gopatch.DoWork += new DoWorkEventHandler(gopatch_DoWork);
gopatch.RunWorkerCompleted += new RunWorkerCompletedEventHandler(gopatch_RunWorkerCompleted);
gopatch.RunWorkerAsync();

但这种方式使用的方式太多cpu。有没有办法让它使用更少的CPU但是按照我的意愿完成工作?

1 个答案:

答案 0 :(得分:1)

您可以在while循环中使用Thread.Sleep方法来防止CPU消耗过多。它不是最佳的,但它很简单。如果您需要更好的东西,请尝试使用事件。 This page介绍了如何使用ManagementEventWatcher来执行此操作。