杀死所有产卵的铬

发布于 2025-01-24 04:22:05 字数 1079 浏览 1 评论 0原文

我有一个具有以下方法的项目。

using System.Management;

        public void KillAllSpawnedChromes() {
            var chromeProcs = GetMyChildChromeProcesses();

            _logger.Info("Found {0} chrome processess still running. Killing them", chromeProcs.Count());
            foreach (var chromeProc in chromeProcs) {
                chromeProc.Kill();
            }
        }

        private static IEnumerable<Process> GetMyChildChromeProcesses() {
            var myCurrentProcess = Process.GetCurrentProcess();
            var children = new List<Process>();
            var mos = new ManagementObjectSearcher(
                $"Select * From Win32_Process Where ParentProcessID={myCurrentProcess.Id}");

            foreach (var o in mos.Get()) {
                var mo = (ManagementObject)o;
                children.Add(Process.GetProcessById(Convert.ToInt32(mo["ProcessID"])));
            }

            return children.Where(x => x.ProcessName.Contains("chrome"));
        }
    }

我想从system.managment中删除这种依赖性。还有其他方法可以杀死镀铬过程吗?

I have a project that has the following methods.

using System.Management;

        public void KillAllSpawnedChromes() {
            var chromeProcs = GetMyChildChromeProcesses();

            _logger.Info("Found {0} chrome processess still running. Killing them", chromeProcs.Count());
            foreach (var chromeProc in chromeProcs) {
                chromeProc.Kill();
            }
        }

        private static IEnumerable<Process> GetMyChildChromeProcesses() {
            var myCurrentProcess = Process.GetCurrentProcess();
            var children = new List<Process>();
            var mos = new ManagementObjectSearcher(
                
quot;Select * From Win32_Process Where ParentProcessID={myCurrentProcess.Id}");

            foreach (var o in mos.Get()) {
                var mo = (ManagementObject)o;
                children.Add(Process.GetProcessById(Convert.ToInt32(mo["ProcessID"])));
            }

            return children.Where(x => x.ProcessName.Contains("chrome"));
        }
    }

I would like to delete this dependency from System.Managment. Is there any other way to kill Chrome processes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空‖城人不在 2025-01-31 04:22:05

您可以使用以下

//  from https://code-examples.net/en/q/60640
public static class ProcessExtensions
{
    private static string FindIndexedProcessName(int pid)
    {
        try
        {
            var processName = Process.GetProcessById(pid).ProcessName;
            var processesByName = Process.GetProcessesByName(processName);
            string processIndexdName = null;

            for (var index = 0; index < processesByName.Length; index++)
            {
                processIndexdName = index == 0 ? processName : processName + "#" + index;
                var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
                if ((int)processId.NextValue() == pid)
                {
                    return processIndexdName;
                }
            }

            return processIndexdName;
        }
        catch(Exception)
        {
            return "";
        }
    }

    private static Process FindPidFromIndexedProcessName(string indexedProcessName)
    {
        try
        {
            var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
            return Process.GetProcessById((int)parentId.NextValue());
        }
        catch(Exception)
        {
            return null;
        }
    }

    public static Process Parent(this Process process)
    {
        return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
    }
}

现在相当简单:

Process[] processes = Process.GetProcessesByName("chrome");
int pid = Process.GetCurrentProcess().Id;

foreach (Process process in processes)
{
   var parent = process.Parent();

   if ((parent != null) && (pid == parent.Id))
   {
       process.Kill();
   }
}

请注意, Chrome 浏览器的一个实例通常会产生几个过程。可能有必要杀死它们或以特定顺序正确关闭它们。除非您小心,否则可能会发生 chrome chrome 数据文件的损坏。

You could use the following Extension Method:

//  from https://code-examples.net/en/q/60640
public static class ProcessExtensions
{
    private static string FindIndexedProcessName(int pid)
    {
        try
        {
            var processName = Process.GetProcessById(pid).ProcessName;
            var processesByName = Process.GetProcessesByName(processName);
            string processIndexdName = null;

            for (var index = 0; index < processesByName.Length; index++)
            {
                processIndexdName = index == 0 ? processName : processName + "#" + index;
                var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
                if ((int)processId.NextValue() == pid)
                {
                    return processIndexdName;
                }
            }

            return processIndexdName;
        }
        catch(Exception)
        {
            return "";
        }
    }

    private static Process FindPidFromIndexedProcessName(string indexedProcessName)
    {
        try
        {
            var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
            return Process.GetProcessById((int)parentId.NextValue());
        }
        catch(Exception)
        {
            return null;
        }
    }

    public static Process Parent(this Process process)
    {
        return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
    }
}

The kill loop is now rather simple:

Process[] processes = Process.GetProcessesByName("chrome");
int pid = Process.GetCurrentProcess().Id;

foreach (Process process in processes)
{
   var parent = process.Parent();

   if ((parent != null) && (pid == parent.Id))
   {
       process.Kill();
   }
}

Note that a single instance of the Chrome browser usually spawns several processes. It might be necessary to kill them or properly shut them down in a specific order. Unforeseen damage of the Chrome data files might happen, unless you are careful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文