Is there a way to determine when / when a process starts another process in C # .Net 2.0?

I am looking for a way to find that the process launched by my application in turn generated additional processes. I hope that I have a thread that starts and the application watches until it exits, and then calls other actions when this application exits. The problem is that with some applications that I want to track, they start an additional process and then shut down. so my trigger fires before the application exits.

For example, I run a program in Process. this executable executes some file fixes and then launches the main program - a separate exe - and then exits. this causes Process.HasExited to return true, which fires prematurely. I really care about when the main program exits, in this case the program started by the initial process, but I cannot find a way to do this programmatically.

+4
source share
3 answers

OK, I get it. There is no direct access to the parent information of a process using the Process class in .NET 2.0, but using the System.Diagnostics.PerformanceCounter class, you can access the parent Process ID from the Create Process ID counter name. to solve my problem, I did something similar to:

... /// check to see if the monitored application (started in myProcess) is still running public bool isRunning() { bool running = !this.myProcess.HasExited; if(!running){ foreach (Process p in Process.GetProcesses()) { PerformanceCounter pc = new PerformanceCounter("Process", "Creating Process Id", p.ProcessName); if (this.myProcess.Id == (int)pc.RawValue) { running = true; } } } return running; } ... 

In this example, I did not take into account error handling, and as a more advanced solution you could track the tree of spawned processes, keeping a list of all process identifiers of the branch, this example monitors only one direct child process.

+5
source

If these other processes are property / not within your control, I really don’t see how you can control their verification, whether they give rise to new processes, especially if you want to check whether the processes generated by grandchildren and the generated processes ... (you know where I'm going).

In any case, my opinion, maybe it can be useful, I just do not think that it can be achieved using only .NET / C #.

We hope someone else can provide a real solution.

+1
source

I think this is not very simple, but doable.

In this case, I would prefer to use WMI. First you start by listing the existing processes [ether, WMI or Net Process] and remember this, you will observe. Then you fire two WMI event events using Win32_ProcessStartTrace and then request Win32_ProcessStopTrace [see WMI SDK]. Both return an instance of Win32_Process that always contains "ParentProcessID". If this is one of them, you saved it before, you have a spawned process. "Win32_Process" also contains the "Kill" method: there is no clue what you finally want to do; -)

Hope this helps. Feel free to contact for more information.

w - mabra

0
source

All Articles