C#捕获控制台(console)关闭事件

有时,公司内部自己开发的 控制台(console)应用程序在服务器上运行会因为遇到某些异常自动关闭了,这就需要用某机制来捕获控制台(console)关闭事件,把这样写日志,便于维护和调试。

源码如下:
程序代码 程序代码

Code
 1using System;
 2using System.Runtime.InteropServices;
 3using System.Threading;
 4using System.Diagnostics;
 5
 6namespace xmlpusher
 7{
 8    public delegate bool ConsoleCtrlDelegate(int dwCtrlType);
 9    /**////    <summary>   
10    ///    Class1    的摘要说明。   
11    ///    </summary>   

12    class Class1
13    {
14        //The    SetConsoleCtrlHandler    function    adds    or    removes    an    application-defined    HandlerRoutine    function     
15        //from    the    list    of    handler    functions    for    the    calling    process.   
16        [DllImport("kernel32.dll")]
17        private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate HandlerRoutine, bool Add);
18        //一個Ctrl    +    C的信號被接收,該信號或來自鍵盤,或來自GenerateConsoleCtrlEvent    函數   
19        private const int CTRL_C_EVENT = 0;
20        //一個    Ctrl    +    Break    信號被接收,該信號或來自鍵盤,或來自GenerateConsoleCtrlEvent    函數   
21        private const int CTRL_BREAK_EVENT = 1;
22        //當用戶系統關閉Console時,系統會發送此信號到此   
23        private const int CTRL_CLOSE_EVENT = 2;
24        //當用戶退出系統時系統會發送這個信號給所有的Console程序。該信號不能顯示是哪個用戶退出。   
25        private const int CTRL_LOGOFF_EVENT = 5;
26        //當系統將要關閉時會發送此信號到所有Console程序   
27        private const int CTRL_SHUTDOWN_EVENT = 6;
28        /**////    <summary>   
29        ///    应用程序的主入口点。   
30        ///    </summary>   

31        [STAThread]
32        static void Main(string[] args)
33        {
34            //   
35            //    TODO:    在此处添加代码以启动应用程序   
36            //   
37            Class1 cl = new Class1();
38        }

39
40        public Class1()
41        {
42            ConsoleCtrlDelegate newDategate = new ConsoleCtrlDelegate(HandlerRoutine);
43            bool re = SetConsoleCtrlHandler(newDategate, true);
44            if (re)
45            {
46                Console.WriteLine("Set    SetConsoleCtrlHandler    success!!");
47            }

48            else
49            {
50                Debug.WriteLine("Set    SetConsoleCtrlHandler    Error!!");
51                AsReportFile.WriteFile("","test.txt","who close?");
52            }

53            Console.ReadLine();
54        }

55
56        bool HandlerRoutine(int CtrlType)
57        {
58            switch (CtrlType)
59            {
60                case CTRL_CLOSE_EVENT:
61                    for (int i = 0; i < 100; i++)
62                    {
63                        Console.WriteLine("i    is:{0}", i);
64                        Thread.Sleep(1000);
65                    }

66                    break;
67            }

68            return false;
69        }

70    }

71}

72
73

http://www.techmango.com/blog/article.asp?id=381

转载于:https://www.cnblogs.com/plain-heart/archive/2008/04/17/1158874.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值