If that is possible to edit p1
process to wait the completion of p2
- that will be the simplest solution. In this case you can start p1
in 'no window' mode and wait its completion.
If that is not possible, then you can still find all processes started by p1
by using process parent id:
p1.WaitForExit();
var p1Id = p1.Id;
var subProcesses = Process.GetProcesses().Where(p => {
var indexedName = ProcessExtensions.FindIndexedProcessName(p.Id);
var parentIdCounter = new PerformanceCounter("Process", "Creating Process ID", indexedName);
var parentId = (int)parentIdCounter.NextValue();
return parentId == p1Id;
}).ToArray();
subProcesses.Select(_ => _.Id).ToList().ForEach(Console.WriteLine);
If you know UI application name and there could not be started more than 1 application for the user, then you can ignore parent id check and just find process by its name.