C# 控制台捕获关闭事件的代码

代码如下,记下来积累一下,有不懂的可以留言交流哟~~

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;

namespace ConsoleClose
{
    public delegate bool ControlCtrlDelegate(int CtrlType);
    class Program
    {
        [DllImport("kernel32.dll")]
        private static extern bool SetConsoleCtrlHandler(ControlCtrlDelegate HandlerRoutine, bool Add);

        static ControlCtrlDelegate newDelegate = new ControlCtrlDelegate(HandlerRoutine);

        public static bool HandlerRoutine(int CtrlType)
        {
            switch (CtrlType)
            {
                case 0: 
                    Console.WriteLine("0工具被强制关闭"); //Ctrl+C关闭
                    break;
                case 2:
                    Console.WriteLine("2工具被强制关闭");//按控制台关闭按钮关闭
                    break;
            }
            return false;
        }

        static void Main(string[] args)
        {
            bool bRet = SetConsoleCtrlHandler(newDelegate, true);

            //这后面写程序该做的事情
            while(true)
            {
                Console.WriteLine("请关闭!");
                Thread.Sleep(5000);
            }
        }
    }
}

你可能感兴趣的:(C#,c#,工具,string,class,c)