c# 控制台应用程序中止时如何终止子进程?

发布于 2024-08-19 03:01:43 字数 138 浏览 2 评论 0原文

我有一个控制台应用程序,它使用 WMI ManagementClass 生成其他 win32 进程。当用户通过 proc explorer 或按 ctrl+c 终止控制台应用程序时,我有一个要求,应用程序应终止它创建的所有子进程。什么是最好的实现这个目标的方法?

I have a console application that spawns other win32 processes using WMI ManagementClass.I have a requirement when a user kills the console application through proc explorer or by pressing ctrl+c ,the application should terminate all the child processes it created.What is the best way to achive this?

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

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

发布评论

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

评论(2

葬花如无物 2024-08-26 03:01:43

请记住,您必须考虑您的需求,您可以按照下面的示例进行操作。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace KillSpawnedProcesses
{
    class Program
    {
        static List<int> _processes = new List<int>();

        static void Main(string[] args)
        {
            Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);

            StartProcesses();
            Console.Read(); //to hold up console
            Console.Read(); //to hold up console
        }

        static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            KillProcesses();
        }

        static void StartProcesses()
        {
            for(int i = 0; i < 2; i++)
            {
                Process p = new Process();
                p.StartInfo = new ProcessStartInfo();
                p.StartInfo.FileName = "Notepad.exe";
                p.Start();
                _processes.Add(p.Id);
            }
        }

        static void KillProcesses()
        {
            foreach(var p in _processes)
            {
                Process tempProcess = Process.GetProcessById(p);
                tempProcess.Kill();
            }            
        }
    }
}

Keeping in mind that you have to take in your needs into account, you can do it like the sample below.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace KillSpawnedProcesses
{
    class Program
    {
        static List<int> _processes = new List<int>();

        static void Main(string[] args)
        {
            Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);

            StartProcesses();
            Console.Read(); //to hold up console
            Console.Read(); //to hold up console
        }

        static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            KillProcesses();
        }

        static void StartProcesses()
        {
            for(int i = 0; i < 2; i++)
            {
                Process p = new Process();
                p.StartInfo = new ProcessStartInfo();
                p.StartInfo.FileName = "Notepad.exe";
                p.Start();
                _processes.Add(p.Id);
            }
        }

        static void KillProcesses()
        {
            foreach(var p in _processes)
            {
                Process tempProcess = Process.GetProcessById(p);
                tempProcess.Kill();
            }            
        }
    }
}
别想她 2024-08-26 03:01:43

如果子进程有消息队列(Win32消息泵),您可以将WM_CLOSE发送到其主窗口,或者定义您自己的消息。否则,您可以使用套接字、管道或事件等同步对象来设计进程间通知。

最糟糕的方法是终止子进程。

If the sub-process has a message queue (Win32 message pumping), you can post WM_CLOSE to its main window, or define your own message. Otherwise, you can design your inter-process notification by using Sockets, Pipes, or Synchronization objects like Events.

The worst way is to kill the sub-processes.

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