using System;
using System
using System
using System
namespace ConsoleColsed
{
public delegate bool ConsoleCtrlDelegate(int dwCtrlType);
public class ClsMain
{
[DllImport(
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate HandlerRoutine
//當用戶關閉Console時
private const int CTRL_CLOSE_EVENT =
[stathread]
static void Main()
{
ClsMain cls=new ClsMain();
}
public ClsMain()
{
// 用API安裝事件處理
ConsoleCtrlDelegate newDelegate=new ConsoleCtrlDelegate(HandlerRoutine);
bool bRet=SetConsoleCtrlHandler(newDelegate
if(bRet==false) //安裝事件處理失敗
{
Debug
}
else
{
Console
Console
}
}
/**//// <summary>
/// 處理消息的事件
/// </summary>
private static bool HandlerRoutine(int CtrlType)
{
switch(CtrlType)
{
case CTRL_CLOSE_EVENT: //用戶要關閉Console了
Debug
break;
}
return false;
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/11665.html