在C#中以编程方式杀死进程树 [英] Kill process tree programmatically in C#

查看:107
本文介绍了在C#中以编程方式杀死进程树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码以编程方式启动Internet Explorer:

  ProcessStartInfo startInfo = new ProcessStartInfo( iexplore.exe ); 
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = http://www.google.com;
Process ieProcess = Process.Start(startInfo);

这将生成2个在Windows任务管理器中可见的进程。然后,我尝试使用以下命令终止该进程:

  ieProcess.Kill(); 

这将导致Task Manager中的一个进程被关闭,而另一个仍然存在。我尝试检查任何具有子进程的属性,但均未找到。我如何也可以杀死其他进程?更一般而言,如何杀死与以Process.Start开头的进程相关联的所有进程?

解决方案

  ///< summary> 
///杀死一个进程,以及它的所有子代,孙代等。
///< / summary>
///< param name = pid>进程ID。< / param>
private static void KillProcessAndChildren(int pid)
{
//无法关闭系统空闲进程。
if(pid == 0)
{
return;
}
ManagementObjectSearcher searcher = new ManagementObjectSearcher
( Select * From Win32_Process where ParentProcessID = + pid);
ManagementObjectCollection moc = searcher.Get();
foreach(moc中的ManagementObject mo)
{
KillProcessAndChildren(Convert.ToInt32(mo [ ProcessID])));
}
试试
{
Process proc = Process.GetProcessById(pid);
proc.Kill();
}
catch(ArgumentException)
{
//进程已退出。
}
}



更新2016-04-26



Win7 x64 Visual Studio 2015 Update 2 上测试。



更新2017年11月14日



已添加支票对于系统空闲进程 if(pid == 0)



更新2018-03-02



需要添加对 System.Management 命名空间的引用,请参见下面@MinimalTech的注释。如果您安装了ReSharper,它将自动为您执行此操作。



更新2018-10-10



<最常见的用例是杀死我们自己的C#进程已启动的所有子进程。



在这种情况下,更好的解决方案是在C#中使用Win32调用,使任何生成的进程成为子进程。这意味着在退出父进程时,Windows将自动关闭所有子进程,从而无需使用上面的代码。如果您要我发布代码,请告诉我。


I am starting Internet Explorer programmatically with code that looks like this:

ProcessStartInfo startInfo = new ProcessStartInfo("iexplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "http://www.google.com";
Process ieProcess = Process.Start(startInfo);

This generates 2 processes visible in the Windows Task Manager. Then, I attempt to kill the process with:

ieProcess.Kill();

This results in one of the processes in Task Manager being shut down, and the other remains. I tried checking for any properties that would have children processes, but found none. How can I kill the other process also? More generally, how do you kill all the processes associated with a process that you start with Process.Start?

解决方案

This worked very nicely for me:

/// <summary>
/// Kill a process, and all of its children, grandchildren, etc.
/// </summary>
/// <param name="pid">Process ID.</param>
private static void KillProcessAndChildren(int pid)
{
    // Cannot close 'system idle process'.
    if (pid == 0)
    {
        return;
    }
    ManagementObjectSearcher searcher = new ManagementObjectSearcher
            ("Select * From Win32_Process Where ParentProcessID=" + pid);
    ManagementObjectCollection moc = searcher.Get();
    foreach (ManagementObject mo in moc)
    {
        KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
    }
    try
    {
        Process proc = Process.GetProcessById(pid);
        proc.Kill();
    }
    catch (ArgumentException)
    {
        // Process already exited.
    }
}

Update 2016-04-26

Tested on Visual Studio 2015 Update 2 on Win7 x64. Still works as well now as it did 3 years ago.

Update 2017-11-14

Added check for system idle process if (pid == 0)

Update 2018-03-02

Need to add a reference to the System.Management namespace, see comment from @MinimalTech below. If you have ReSharper installed, it will offer to do this for you automatically.

Update 2018-10-10

The most common use case for this is killing any child processes that our own C# process has started.

In this case, a better solution is to use Win32 calls within C# to make any spawned process a child process. This means that when the parent process exits, any child processes are automatically closed by Windows, which eliminates the need for the code above. Please let me know if you want me to post the code.

这篇关于在C#中以编程方式杀死进程树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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