监视子进程 [英] Monitoring child process

查看:125
本文介绍了监视子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我一直跟着坐着.一个应用程序用于使用另一应用程序为多个计算设置数据.所以现在就这样:

Hi
I''ve following sittuation. One app is used to set up data for multiple calculations using another app. So for now it do it like that:

Read input data
setup child process P
while p.HasExited = false
   if killProcess = true Then kill process and exit
   Application.DoEvents()


它可以工作,但是循环消耗了50%的CPU

我可以使用WaitForExit,但是它会阻塞UI ,因此可能需要将其放入后台工作程序中.
您知道其他解决方法吗?


It works but the loop is consuming 50% CPU

I could use WaitForExit but it blocks the UI so probably I need to put it in Background worker.
Do you know any other way to solve this?

推荐答案

您尝试过Process.Exited事件吗?
只需在开始子进程之前向该事件添加处理程序即可.

顺便说一句:从不不要使用Application.DoEvents.您可能会有一些不可预测的行为...
Did you try Process.Exited event?
Just add a handler to that event just before starting your child process.

By the way: never use Application.DoEvents. You might have some unpredictable behaviors...


当然. (您必须使用双核CPU,否则将是100%.)根据经验,切勿使用Application.DoEvents,也不要使用旋转等待(循环等待).使用单独的线程和阻塞调用System.Diagnostics.Process.WaitForExit.线程将使用零CPU时间保持在等待状态,直到唤醒为止.这是在CPU级别完成的.调用线程已关闭,并且从未调度过,直到该过程完成为止.然后,线程被唤醒并立即继续执行(更确切地说,根据这些线程和竞争线程的优先级,计划将其很快执行).

—SA
Of course. (You must be using Dual Core CPU, otherwise it would be 100%.) As a rule of thumb, never use Application.DoEvents and never use spin wait (waiting with loop). Use a separate thread and a blocking call System.Diagnostics.Process.WaitForExit. The thread will be kept in a wait state using zero CPU time until awaken. This is done at the level of CPU. The calling thread switched off and never scheduled again until the process finished. Then the thread is awaken and immediately continue execution (well, more exactly, scheduled for execution very quickly, depending on the priorities of these and competing threads).

—SA


这篇关于监视子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆