[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_SHUTDOWN_EVENT:
                case CtrlType.CTRL_CLOSE_EVENT:
                    {
                        Console.WriteLine("= exit =");
                        return false;
                    }
                case CtrlType.CTRL_LOGOFF_EVENT:
                default:
                    return false;
            }
        }

        static void Main(string[] args)
        {
            // exit event handler
            // Some biolerplate to react to close window event
            _handler += new EventHandler(Handler);
            SetConsoleCtrlHandler(_handler, true);
        }


參考資料
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JohnDX 的頭像
    JohnDX

    我要回家想一想~

    JohnDX 發表在 痞客邦 留言(0) 人氣()