C#捕获控制台(console)关闭事件
有时,公司内部自己开发的控制台(console)应用程序在服务器上运行会因为遇到某些异常自动关闭了,这就需要用某机制来捕获控制台(console)关闭事件,把这样写日志,便于维护和调试。
源码如下:
程序代码
![]()
Code
1
using System;
2
using System.Runtime.InteropServices;
3
using System.Threading;
4
using System.Diagnostics;
5![]()
6
namespace 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
源码如下:
程序代码1
using System;2
using System.Runtime.InteropServices;3
using System.Threading;4
using System.Diagnostics;5

6
namespace xmlpusher7


{8
public delegate bool ConsoleCtrlDelegate(int dwCtrlType);9

/**//// <summary> 10
/// Class1 的摘要说明。 11
/// </summary> 12
class Class113

{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
else49

{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
浙公网安备 33010602011771号