using 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
https://stackoverflow.com/questions/4646827/on-exit-for-a-console-application
最后
以上就是年轻缘分最近收集整理的关于C#控制台关闭之前做一些操作的全部内容,更多相关C#控制台关闭之前做一些操作内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复