首页 > Windows开发 > 详细

C#控制台关闭之前做一些操作

时间:2017-11-21 11:25:59      阅读:281      评论:0      收藏:0      [点我收藏+]
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#控制台关闭之前做一些操作

原文:http://www.cnblogs.com/Lulus/p/7872261.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!