簡體   English   中英

Windows 7中的Capture Console退出C#

[英]Capture console exit C# in windows 7

有誰知道如何對Windows中C#控制台中的ctrl + c事件做出反應?

這個問題: 捕獲控制台出口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_LOGOFF_EVENT = 5,
        CTRL_SHUTDOWN_EVENT
    }

您需要將Console.CancelKeyPress事件連接到處理程序。 是一篇有關該主題的出色文章。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM