c# - 在 Windows 7 中捕获控制台退出 C#

标签 c# .net events console exit

有谁知道如何在 Windows 的 C# 控制台中对 ctrl+c 事件使用react?

这个问题:Capture console exit C#说了如何去做,但我试过了,它只在用户单击控制台窗口顶部的关闭 X 时捕获事件。

当用户键入 ctrl+c 时没有任何反应,调试时它甚至没有命中处理程序。

谢谢

这是我的代码

namespace EventCloseConsole
{
    using System.Runtime.InteropServices;
    using System;

    class Program
    {
        [DllImport("Kernel32")]
        private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);

        private delegate bool EventHandler(CtrlType sig);
        static EventHandler _handler;

        enum CtrlType
        {
            CTRL_C_EVENT = 0,
            CTRL_BREAK_EVENT = 1,
            CTRL_CLOSE_EVENT = 2,
            CTRL_LOGOFF_EVENT = 5,
            CTRL_SHUTDOWN_EVENT = 6
        }

        private static bool Handler(CtrlType sig)
        {
            switch (sig)
            {
                case CtrlType.CTRL_C_EVENT:
                case CtrlType.CTRL_LOGOFF_EVENT:
                case CtrlType.CTRL_SHUTDOWN_EVENT:
                case CtrlType.CTRL_CLOSE_EVENT:

                    Console.WriteLine("Closing");
                    System.Threading.Thread.Sleep(500);
                    return false;
                default:
                    return true;
            }
        }

        static void Main(string[] args)
        {

            _handler += new EventHandler(Handler);
            SetConsoleCtrlHandler(_handler, true);
            Console.ReadLine();


        }
    }
}

最佳答案

这对我适用于 Windows 7。使用 x 按钮关闭
secret 是变量 static ConsoleEventDelegate _d

private static void Main(string[] args)
{
    ConsoleEventHooker.Closed += ConsoleEventHooker_Closed;
}

static void ConsoleHooker_Closed(object sender, EventArgs e)
{
}

ConsoleEventHooker.cs

namespace System
{
    internal static class ConsoleEventHooker
    {
        private static bool _initedHooker;
        private static EventHandler _closed; 
        private static EventHandler _shutdown;
        private static ConsoleEventDelegate _d;

        public static event EventHandler Closed
        {
            add
            {
                Init();
                _closed += value;
            }
            remove { _closed -= value; }
        }

        public static event EventHandler Shutdown
        {
            add
            {
                Init();
                _shutdown += value;
            }
            remove { _shutdown -= value; }
        }

        private static void Init()
        {
            if (_initedHooker) return;
            _initedHooker = true;
            _d = ConsoleEventCallback;
            SetConsoleCtrlHandler(_d, true);
        }

        private static bool ConsoleEventCallback(CtrlTypes eventType)
        {
            if (eventType == CtrlTypes.CTRL_CLOSE_EVENT)
            {
                if(_closed != null) _closed(null,new EventArgs());
            }

            if (eventType == CtrlTypes.CTRL_SHUTDOWN_EVENT)
            {
                if (_shutdown != null) _shutdown(null, new EventArgs());
            }
            return false;
        }

        // A delegate type to be used as the handler routine 
        // for SetConsoleCtrlHandler.
        delegate bool ConsoleEventDelegate(CtrlTypes ctrlType);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);

    }

    // An enumerated type for the control messages
    // sent to the handler routine.
    public enum CtrlTypes
    {
        CTRL_C_EVENT = 0,
        CTRL_BREAK_EVENT,
        CTRL_CLOSE_EVENT,
        CTRL_LOGOFF_EVENT = 5,
        CTRL_SHUTDOWN_EVENT
    }

关于c# - 在 Windows 7 中捕获控制台退出 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4773828/

相关文章:

c# - 使用多线程为递归类实例创建新对象

c# - 将现有 PdfDocument 保存到文件

c# - 获取 IObservable<string> 对象作为对 POST 请求的响应,如何获取字符串本身?

c# - 条件表达式中的赋值 - 这对代码质量有哪些潜在影响?

.net - 如何创建 MSA ID 和密码以使用 SDK 创建 Azure Bot 服务

java - 关键事件不起作用

c# - 如何将 DbContext(或任何其他对象)注入(inject)对话框?

c# - 如何使用 RefIdStr 和 RavenDB 扩展 ServiceStack UserAuth

events - 如果任务未完成,jBPM 6.2.0 在 1 天时间间隔后发送重复任务提醒

javascript - 如何将事件绑定(bind)到 iframe 中的元素?