1 using System;
 2 using System.Runtime.InteropServices;
 3 using System.Threading;
 4 
 5 namespace ConsoleApp1
 6 {
 7     public delegate bool ControlCtrlDelegate(int ctrlType);
 8 
 9     public class Program
10     {
11         private static readonly ControlCtrlDelegate NewDelegate = HandlerRoutine;
12 
13         [DllImport("kernel32.dll")]
14         private static extern bool SetConsoleCtrlHandler(ControlCtrlDelegate handlerRoutine, bool add);
15 
16         public static bool HandlerRoutine(int ctrlType)
17         {
18             switch (ctrlType)
19             {
20                 case 0:
21                     Console.WriteLine("0工具被强制关闭"); //Ctrl+C关闭  
22                     break;
23                 case 2:
24                     Console.WriteLine("2工具被强制关闭"); //按控制台关闭按钮关闭  
25                     break;
26             }
27             return false;
28         }
29 
30         public static void Main(string[] args)
31         {
32             var bRet = SetConsoleCtrlHandler(NewDelegate, true);
33 
34             for (var i = 0; i < 10000; i++)
35             {
36                 Console.Clear();
37                 Console.WriteLine(i);
38                 Console.WriteLine(i + 1);
39                 Console.WriteLine(i + 2);
40                 Console.WriteLine(i + 3);
41                 Console.WriteLine(i + 4);
42                 Console.WriteLine(i + 5);
43                 Console.WriteLine(i + 6);
44                 Thread.Sleep(100);
45             }
46         }
47     }
48 }

 

posted on 2016-07-20 13:43  俗剑仙·情如梦  阅读(1046)  评论(0)    收藏  举报