监控进程的子进程 [英] Monitor child processes of a process

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

问题描述

我正在使用此代码运行 .exe 文件:

I'm running .exe file using this code:

Process proc = Process.Start("c:program.exe");
proc.WaitForExit();

如果我在启动进程之前启动 Stopwatch 并在 proc.WaitForExit(); 行之后停止它,我可以获得用户使用该特定程序的时间.

If I start Stopwatch before starting the process and stop it after proc.WaitForExit(); line, I can get the time that user was using that particular program.

我面临的问题是某些程序(和游戏)使用启动器 - 一些小的 .exe 文件,通常会检查某些内容,然后启动另一个 .exe 文件,该文件实际上是用户想要运行的程序/游戏.在这些情况下,上面的代码不起作用,因为它在启动器存在后返回.

The problem I'm facing is that some programs (and games) use launchers - some small .exe file that usually checks something and then launches another .exe file that is actually the program/game that the user wants to run. In these cases the code above doesn't work because it returns after launcher exists.

如何跟踪 proc 运行的所有进程,并等待所有进程都终止?

How can I track all processes that proc runs, and wait unitl all of them are terminated?

推荐答案

看看这个 - 查找我自己的 .NET 进程的所有子进程/找出给定进程是否是我自己的子进程?http://social.msdn.microsoft.com/Forums/vstudio/en-US/d60f0793-cc92-48fb-b867-dd113dabcd5c/how-to-find-the-child-processes-related-with-a-pid.它们提供了通过父 PID(您拥有)查找子进程的方法.

Take a look at this - Find all child processes of my own .NET process / find out if a given process is a child of my own? or http://social.msdn.microsoft.com/Forums/vstudio/en-US/d60f0793-cc92-48fb-b867-dd113dabcd5c/how-to-find-the-child-processes-associated-with-a-pid. They provide ways to find child processes by a parent PID (which you have).

您可以编写监控您创建的进程并获取其子进程.然后,您可以跟踪所有内容,并等待它们全部完成.我说尝试"是因为我不确定你是否能捕捉到非常快速的变化(一个开始别人,然后在你得到他的孩子之前死去的过程).

You can write monitor the process you create and also get its children. You could then track everything, and wait for them all to finish. I say "try" because I'm not sure you could catch very rapid changes (a process starting others and then dying before you get his children).

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

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