0

I'm trying to receive the close event from a process, which I start with

p1.EnableRaisingEvents = True
AddHandler p1.Exited, AddressOf p1Exited
System.Diagnostics.Process.Start(p1)

However, p1 itself starts a new process p2, then closes immediately and the close event is fired. The process p2 has a GUI and keeps on running. p2 can only be started from p1. I want to get the close event from p2. Is this somehow possible?

PSt
  • 15
  • 5

1 Answers1

0

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.

fenixil
  • 2,106
  • 7
  • 13