浅析 C# 控制台的 Ctrl+C 是怎么玩的

一:背景

1. 讲故事

上一篇我们聊到了 Console 为什么会卡死,读过那篇文章的朋友相信对 conhost.exe 有了一个大概的了解,这一篇更进一步聊一聊窗口的特殊事件 Ctrl+C 底层流转到底是什么样的,为了方便讲述,让 chagtgpt 给我生成一段Ctrl+C 的业务代码。


    class Program
    {
        static void Main(string[] args)
        {
            Console.CancelKeyPress += new ConsoleCancelEventHandler(CancelKeyPressHandler);

            Console.WriteLine("按下 Ctrl+C 试试看!");

            while (true)
            {
                // 这里写你的代码
            }
        }

        static void CancelKeyPressHandler(object sender, ConsoleCancelEventArgs e)
        {
            Console.WriteLine("你按下了 Ctrl+C!");
            // 在这里可以添加你希望执行的代码
            e

猜你喜欢

转载自blog.csdn.net/m0_70960708/article/details/143478004