寻找我自己的.NET进程的所有子进程/找出一个给定的过程是我自己的孩子呢? [英] Find all child processes of my own .NET process / find out if a given process is a child of my own?

查看:175
本文介绍了寻找我自己的.NET进程的所有子进程/找出一个给定的过程是我自己的孩子呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET类库旋转起来是不停地跑,直到我处理的对象的二次加工。

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

由于程序萦绕在内存中的一些occurances,我决定加入一个集成测试,以确保如果我让对象时隔GC /定稿,这个过程被降速。

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.

然而,由于过程是水银命令行客户端,我的构建服务器已经运行水银作为自己行动的一部分,我想象那里Mercurial是已经运行情况下的测试开始的时候,或者说,它已启动,并且仍在运行,当测试完成后,相对于构建服务器,而不是我的测试。

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.

所以,问题是这样的:

  • 如何找出如果水银客户端,我看着被我进程启动?

通过看,我一直在寻找使用 Process.GetProcesses < /一>方法,但这不作要求。

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.

我找到了这个网页:我怎么知道的父进程ID一个进程?,但似乎我必须给它的进程名。如果我只是给它HG,是不是这个问题太暧昧了我要找的情况?

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?

推荐答案

,因为它发生了,我有一点C#/ WMI code躺在附近,杀死被指定的进程ID产生的所有进程,递归。杀显然不是你想要的,但子进程的结论似乎是你感兴趣的,我希望这是有益的:

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();
                }
            }
        }
    }

这篇关于寻找我自己的.NET进程的所有子进程/找出一个给定的过程是我自己的孩子呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆