我是靠谱客的博主 年轻缘分,这篇文章主要介绍C#控制台关闭之前做一些操作,现在分享给大家,希望可以做个参考。

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

最后

以上就是年轻缘分最近收集整理的关于C#控制台关闭之前做一些操作的全部内容,更多相关C#控制台关闭之前做一些操作内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(114)

评论列表共有 0 条评论

立即
投稿
返回
顶部