【问题标题】:Capture console exit C# in windows 7在 Windows 7 中捕获控制台退出 C#
【发布时间】:2011-01-23 12:39:20
【问题描述】:

有谁知道如何在 windows 中的 c# 中的控制台中对 ctrl+c 事件做出反应?

这个问题: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();


        }
    }
}

【问题讨论】:

  • 调试器妨碍了它,它也在寻找 Ctrl+C。用 Ctrl+F5 启动你的程序来测试一下。

标签: c# .net events console exit


【解决方案1】:

这适用于我在 Windows 7 上。使用 x 按钮关闭
秘密是变量静态 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
    }

【讨论】:

    【解决方案2】:

    您需要将 Console.CancelKeyPress 事件连接到处理程序。 Here 是关于该主题的精彩文章。

    【讨论】:

    • 不幸的是,这对我不起作用,我将代码直接复制并粘贴到一个新的控制台应用程序中。我注意到这是在 2006 年写的,可能是因为我使用的是 windows 7 吗?
    • 那应该是正确的,您可以将您尝试的代码添加到您的问题中。应该只需要添加 Console.CancelKeyPress += new ConsoleCancelEventHandler(myHandler);到你的主要方法
    • 该链接现在已断开。
    • 添加了一个 msdn 链接。
    猜你喜欢
    • 2010-10-03
    • 1970-01-01
    • 2014-08-15
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    相关资源
    最近更新 更多