加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 01:52:00 所属栏目:Windows 来源:网络整理
导读:有没有人知道如何在 Windows中的c#中的控制台中对ctrl c事件作出反应? 这个问题:Capture console exit C#说怎么做,但我已经尝试过它只捕获事件,当用户点击控制台窗口顶部的关闭X. 当用户键入ctrl c时没有任何反应,它甚至在调试时都没有命中处理程序. 谢谢
有没有人知道如何在 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();


        }
    }
}

解决方法

这适用于Windows 7.使用x按钮关闭
秘密是变量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_SHUTDOWN_EVENT
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读