だから、トリックは、すべての子プロセスを検出する最初のものです:
var mos = new ManagementObjectSearcher($"Select * From Win32_Process Where ParentProcessID={process.Id}");
その後、我々はループ内でそれらを収集し、実行時間を測定するための新しいTask
を開始することができます。最後のループでTask
に経過時間を列挙して表示します。
public Tuple<int, TimeSpan> MonitorProcess(Process process)
{
Stopwatch stopwatch = Stopwatch.StartNew();
process.WaitForExit();
stopwatch.Stop();
return Tuple.Create(process.Id, stopwatch.Elapsed);
}
public void LaunchProcess(String processFullPath)
{
try
{
var tasks = new List<Task<Tuple<int,TimeSpan>>>();
Process process = Process.Start(processFullPath);
if (process == null) return;
// Add my current (parent) process
tasks.Add(Task.Factory.StartNew(()=>this.MonitorProcess(process)));
var childProcesses = new List<Process>();
while (!process.HasExited)
{
// Find new child-processes
var mos = new ManagementObjectSearcher($"Select * From Win32_Process Where ParentProcessID={process.Id}");
List<Process> newChildren = mos.Get().Cast<ManagementObject>().Select(mo => new { PID = Convert.ToInt32(mo["ProcessID"]) })
.Where(p => !childProcesses.Exists(cp => cp.Id == p.PID)).Select(p => Process.GetProcessById(p.PID)).ToList();
// measure their execution time in different task
tasks.AddRange(newChildren.Select(newChild => Task.Factory.StartNew(() => this.MonitorProcess(newChild))));
childProcesses.AddRange(newChildren);
}
// Print the results
StringBuilder sb = new StringBuilder();
foreach (Task<Tuple<int, TimeSpan>> task in tasks) {
sb.AppendLine($"[{task.Result.Item1}] - {task.Result.Item2}");
}
this.output.WriteLine(sb.ToString());
}
catch (Exception ex)
{
}
}
私の友人は天才です!どうもありがとうございました!! –
喜んで私は@ItamarLevyを助けることができました。これまたは任意の回答があなたの質問を解決した場合は、チェックマークをクリックして[受諾する]ことを検討してください(http://meta.stackexchange.com/q/5234/179419)。これは、あなたが解決策を見つけ出し、回答者とあなた自身の両方に評判を与えていることを広範なコミュニティに示します。これを行う義務はありません。 –