C#控制台关闭之前做一些操作
2021-04-27 03:27
标签:imp garbage dea error stat shu rgs 控制 service 参考资料: C#控制台关闭之前做一些操作 标签:imp garbage dea error stat shu rgs 控制 service 原文地址:http://www.cnblogs.com/Lulus/p/7872261.htmlusing System;
using System.Runtime.InteropServices;
class Program
{
static void Main(string[] args)
{
handler = new ConsoleEventDelegate(ConsoleEventCallback);
SetConsoleCtrlHandler(handler, true);
Console.ReadLine();
}
static bool ConsoleEventCallback(int eventType)
{
if (eventType == 2) {
Console.WriteLine("Console window closing, death imminent");
}
return false;
}
static ConsoleEventDelegate handler; // Keeps it from getting garbage collected
// Pinvoke
private delegate bool ConsoleEventDelegate(int eventType);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
}
https://stackoverflow.com/questions/4646827/on-exit-for-a-console-application
下一篇:c#打包文件解压缩