Find all child processes of my own .NET process / find out if a given process is a child of my own?
Asked Answered
C

3

14

I have a .NET class library that spins up a secondary process which is kept running until I dispose of the object.

Due to some occurances of the program lingering in memory, I've decided to add an integration test to ensure that if I let the object lapse to GC/Finalization, that process is spun down.

However, since the process is the Mercurial command line client, and my build server is already running Mercurial as part of its own operations, I envision situations where Mercurial is either already running when that test starts, or that it is started, and is still running, when the test finishes, in relation to the build server, and not my test.

So, I want to make sure the Mercurial client I find (or not) is the one I started, and not just any client that is currently running.

So the question is this:

  • How can I find out if the Mercurial client I am looking at was started by my process?

By "looking at", I was looking at using the Process.GetProcesses method, but this is not a requirement.

If a different question is better, "How to find all child processes of my own process", ie. easier to answer, that one is more than enough as well.

I found this page: How I can know the parent process ID of a process?, but it seems that I have to give it the process name. If I just give it "hg", isn't that question too ambiguous for the case I'm looking for?

Cypripedium answered 25/8, 2011 at 10:52 Comment(0)
P
27

as it happens I have a bit of C#/WMI code lying around that kills all processes spawned by a specified process id, recursively. the killing is obviously not what you want, but the finding of child processes seems to be what you're interested in. I hope this is helpful:

    private static void KillAllProcessesSpawnedBy(UInt32 parentProcessId)
    {
        logger.Debug("Finding processes spawned by process with Id [" + parentProcessId + "]");

        // NOTE: Process Ids are reused!
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(
            "SELECT * " +
            "FROM Win32_Process " +
            "WHERE ParentProcessId=" + parentProcessId);
        ManagementObjectCollection collection = searcher.Get();
        if (collection.Count > 0)
        {
            logger.Debug("Killing [" + collection.Count + "] processes spawned by process with Id [" + parentProcessId + "]");
            foreach (var item in collection)
            {
                UInt32 childProcessId = (UInt32)item["ProcessId"];
                if ((int)childProcessId != Process.GetCurrentProcess().Id)
                {
                    KillAllProcessesSpawnedBy(childProcessId);

                    Process childProcess = Process.GetProcessById((int)childProcessId);
                    logger.Debug("Killing child process [" + childProcess.ProcessName + "] with Id [" + childProcessId + "]");
                    childProcess.Kill();
                }
            }
        }
    }
Porett answered 25/8, 2011 at 11:13 Comment(3)
This worked beautifully for me, although I went for a simpler approach using <code>ManagementObject</code> and no searcher. I'd been struggling with killing my subprocesses for a while, thanks!Federalese
Required the System.Management assembly/referenceEpperson
Thank you for saving us time digging in the WMI API documentation for this !!!Usurpation
N
8

Although the answer of mtijn is probably the closest you can get, the real answer is: you can't.

Windows does not maintain a real process tree where processes are re-structured when a process on an intermediate level dies. However, Windows does remember the parent process ID.

The problematic case is the following:

Original process structure:

YourApp.exe
- SubApp.exe
  - Second.exe

If now SubApp.exe terminates, the parent process ID of Second.exe will not be updated. The new result is

YourApp.exe
Second.exe

You can verify this using SysInternals Process Explorer. It is able to show the processes as a tree. Start CMD, then type start cmd. Into the new window, type start cmd again. You'll have 3 windows open. Now terminate the middle one.

Negotiable answered 13/11, 2017 at 22:55 Comment(1)
for those wishing to use the code of my answer this post is a fair warning of its limitations, however the original question was about parent and child processes nested at most 1 level deep, not >1 levels as described here.Porett
C
1

Thanks! From previous KillAllProcessesSpawnedBy Answer I made this:

        public static void WaitForAllToExit(this Process process)
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                "SELECT * " +
                "FROM Win32_Process " +
                "WHERE ParentProcessId=" + process.Id);
            ManagementObjectCollection collection = searcher.Get();
            if (collection.Count > 0)
            {
                foreach (var item in collection)
                {
                    UInt32 childProcessId = (UInt32)item["ProcessId"];
                    if ((int)childProcessId != Process.GetCurrentProcess().Id)
                    {
                        Process childProcess = Process.GetProcessById((int)childProcessId);
                        WaitForAllToExit(childProcess);
                    }
                }
            }

            process.WaitForExit();

        }
Churchill answered 23/4, 2021 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.