日期:2014-05-17 浏览次数:20863 次
class Demo
{
public delegate bool HandlerRoutine(ConsoleControlEventType ctrlType);
public enum ConsoleControlEventType : int
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine handlerRoutine,bool Add);
static bool CtrlProc(ConsoleControlEventType ctrlType)
{
switch (ctrlType)
{
case ConsoleControlEventType.CTRL_C_EVENT:
//ctrl+c
break;
case ConsoleControlEventType.CTRL_CLOSE_EVENT:
//可以做些清理工作
return true;
//....
}
return false;
}
static void Main(string[] args)
{
SetConsoleCtrlHandler(CtrlProc, true);
//
//...其他代码
}
}