问题描述
我用这个code运行.exe文件:
过程PROC =的Process.Start(C:\ PROGRAM.EXE);
proc.WaitForExit();
如果我开始秒表
在启动过程中和之后 proc.WaitForExit停止()之前;
行,我可以使用该特定程序得到的时间为用户
我现在面临的问题是,一些程序(和游戏)使用发射器 - 一些小的.exe文件,通常检查一番,然后启动另一个.exe文件,实际上是程序/游戏,用户要运行。在这种情况下,code以上不工作,因为它返回发射后存在。
我怎么能跟踪所有程序PROC
运行,并等待单元1所有的人都将终止?
编辑:
下面是我的解决办法:
公共静态类ProcessExtensions
{
公共静态的IEnumerable<方法> GetChildProcesses(这个过程工艺)
{
清单<方法>孩子=新的List<方法>();
ManagementObjectSearcher MOS =新ManagementObjectSearcher(的String.Format(SELECT * FROM Win32_Process的其中ParentProcessID = {0},process.Id));
的foreach(的ManagementObject莫mos.Get())
{
children.Add(Process.GetProcessById(Convert.ToInt32(MO [的ProcessID])));
}
返回儿童;
}
}
看看这个 - Find我自己的.NET进程的所有子进程/找出一个给定的过程是我自己的孩子呢?或http://social.msdn.microsoft.com/Forums/vstudio/en-US/d60f0793-cc92-48fb-b867-dd113dabcd5c/how-to-find-the-child-processes-associated-with-a-pid.他们提供的方法来查找父PID子进程(你有)。
您可以编写监视创建过程中,也得到了孩子们。然后,您可以跟踪一切,并等待他们全部完成。我说:试试,因为我不知道你能赶上非常迅速的变化(在这个过程开始别人,然后临死之前,你自己的孩子)。
I'm running .exe file using this code:
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.
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.
How can I track all processes that proc
runs, and wait unitl all of them are terminated?
EDIT:
Here is my solution:
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;
}
}
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).
这篇关于监控的过程的子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!