C# 监控进程的子进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17922725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Monitor child processes of a process
提问by xx77aBs
I'm running .exe file using this code:
我正在使用以下代码运行 .exe 文件:
Process proc = Process.Start("c:\program.exe");
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.
如果我Stopwatch
在开始进程之前开始并在proc.WaitForExit();
一行之后停止它,我可以获得用户使用该特定程序的时间。
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.
我面临的问题是某些程序(和游戏)使用启动器 - 一些小的 .exe 文件通常会检查某些内容,然后启动另一个 .exe 文件,该文件实际上是用户想要运行的程序/游戏。在这些情况下,上面的代码不起作用,因为它在启动器存在后返回。
How can I track all processes that proc
runs, and wait unitl all of them are terminated?
如何跟踪所有proc
运行的进程,并等待所有进程都终止?
采纳答案by Vadim
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).
看看这个 -查找我自己的 .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(您拥有的)查找子进程的方法。
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).
您可以编写监控您创建的进程并获取其子进程。然后,您可以跟踪所有内容,并等待它们全部完成。我说“尝试”是因为我不确定你是否能捕捉到非常快速的变化(一个开始别人,然后在你得到他的孩子之前死去的过程)。
回答by No Idea For Name
you can't wait for process(B) another process(A) is running, if that process(A) isn't waiting for the process(B). what you can do is track the process using Process.GetProcessesByName()
if you know it's name
你不能等待进程(B)另一个进程(A)正在运行,如果该进程(A)没有等待进程(B)。Process.GetProcessesByName()
如果你知道它的名字,你可以做的是跟踪这个过程
回答by ANeves
Here is the solution that the asker found:
这是提问者找到的解决方案:
public static class ProcessExtensions
{
public static IEnumerable<Process> GetChildProcesses(this Process process)
{
List<Process> children = new List<Process>();
ManagementObjectSearcher mos = new ManagementObjectSearcher(String.Format("Select * From Win32_Process Where ParentProcessID={0}", process.Id));
foreach (ManagementObject mo in mos.Get())
{
children.Add(Process.GetProcessById(Convert.ToInt32(mo["ProcessID"])));
}
return children;
}
}