System.Diagnostics.Process.Start怪异的行为怪异、行为、Diagnostics、System

由网友(心跳多久爱多久)分享简介:我正在写一个应用程序来启动和监视在C#中的其他应用程序。我使用的System.Diagnostics.Process类使用Process.Responding属性来轮询应用程序的状态每100 milisecs启动应用程序,然后监视应用程序。我用Process.CloseMainWindow停止应用程序或Process....

我正在写一个应用程序来启动和监视在C#中的其他应用程序。我使用的System.Diagnostics.Process类使用Process.Responding属性来轮询应用程序的状态每100 milisecs启动应用程序,然后监视应用程序。我用Process.CloseMainWindow停止应用程序或Process.Kill杀死它,如果它不响应。

I'm writing an application to start and monitor other applications in C#. I'm using the System.Diagnostics.Process class to start applications and then monitor the applications using the Process.Responding property to poll the state of the application every 100 milisecs. I use Process.CloseMainWindow to stop the application or Process.Kill to kill it if it's not responding.

我注意到一个怪异的行为,其中有时进程对象进入的状态下,响应属性总是甚至当底层程序挂在一个循环,并返回true,它并没有CloseMainWindow回应。

I've noticed a weird behaviour where sometimes the process object gets into a state where the responding property always returns true even when the underlying process hangs in a loop and where it doesn't respond to CloseMainWindow.

重现它的一个方法是在启动流程实例后轮询响应产权。因此,例如

One way to reproduce it is to poll the Responding property right after starting the process instance. So for example

_process.Start();
bool responding = _process.Responding;

将重现错误状态,而

will reproduce the error state while

_process.Start();
Thread.Sleep(1000);
bool responding = _process.Responding;

将工作。 减少睡眠期间500将再次引入错误状态。

will work. Reducing the sleep period to 500 will introduce the error state again.

东西在呼唤_process.Responding后太快开始似乎prevent从得到正确的Windows消息队列处理的对象。我想我需要等待_process.Start来完成这样做的异步工作。有没有更好的方式来等待这个比调用了Thread.Sleep?我不是太相信,1000毫秒永远是不够的。

Something in calling _process.Responding too fast after starting seems to prevent the object from getting the right windows message queue handler. I guess I need to wait for _process.Start to finish doing it's asynchronous work. Is there a better way to wait for this than calling Thread.Sleep ? I'm not too confident that the 1000 ms will always be enough.

推荐答案

现在,我以后需要检查了这一点,但我肯定有一个方法,告诉等待,直到它准备好输入线。你只监测的GUI程序?

Now, I need to check this out later, but I am sure there is a method that tells the thread to wait until it is ready for input. Are you monitoring GUI processes only?

时的 Process.WaitForInputIdle 不是任何帮助吗?还是我错过了点? :)

Isn't Process.WaitForInputIdle of any help to you? Or am I missing the point? :)

继在Twitter上闲聊(或鸣叫鸣叫?)有Men​​delt我想我应该更新我的回答让社会各界充分认识。

Following a chit-chat on Twitter (or tweet-tweet?) with Mendelt I thought I should update my answer so the community is fully aware..

WaitForInputIdle 将仅适用于具有GUI应用程序。 您指定的等待时间,并且该方法返回一个布尔值,如果过程达到该时间范围内处于闲置状态,可以很明显的使用这个循环如果需要,或处理适当。 WaitForInputIdle will only work on applications that have a GUI. You specify the time to wait, and the method returns a bool if the process reaches an idle state within that time frame, you can obviously use this to loop if required, or handle as appropriate.

希望有所帮助:)

阅读全文

相关推荐

最新文章