开发者

Graceful shutdown of console apps in Win Vista, 7, 2008

开发者 https://www.devze.com 2023-03-17 07:55 出处:网络
How do I handle th开发者_如何学编程e close/x button in console apps when running in Windows Vista, 7, 2008?

How do I handle th开发者_如何学编程e close/x button in console apps when running in Windows Vista, 7, 2008?

I found that I can capture the event by using SetConsoleControlHandler but Windows force terminates the app after a second (or milliseconds). That second isn't enough for my app to clean up.


If you detach the console in response, does it still close you down?

http://msdn.microsoft.com/en-us/library/ms683150(v=vs.85).aspx


You can find out how to detect the application wants to close:

http://geekswithblogs.net/mrnat/archive/2004/09/23/11594.aspx

namespace Detect_Console_Application_Exit2
{
    class Program
    {
        private static bool isclosing = false;
        static void Main(string[] args)
        {
            SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);

            Console.WriteLine("CTRL+C,CTRL+BREAK or suppress the application to exit");
            while (!isclosing) ;

        }

        private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
        {
            // Put your own handler here
            switch (ctrlType)
            {
                case CtrlTypes.CTRL_C_EVENT:
                    isclosing = true;
                    Console.WriteLine("CTRL+C received!");
                    break;

                case CtrlTypes.CTRL_BREAK_EVENT:
                    isclosing = true;
                    Console.WriteLine("CTRL+BREAK received!");
                    break;

                case CtrlTypes.CTRL_CLOSE_EVENT:
                    isclosing = true;
                    Console.WriteLine("Program being closed!");
                    break;

                case CtrlTypes.CTRL_LOGOFF_EVENT:
                case CtrlTypes.CTRL_SHUTDOWN_EVENT:
                    isclosing = true;
                    Console.WriteLine("User is logging off!");
                    break;

            }
            return true;
        }



        #region unmanaged
        // Declare the SetConsoleCtrlHandler function
        // as external and receiving a delegate.

        [DllImport("Kernel32")]
        public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);

        // A delegate type to be used as the handler routine
        // for SetConsoleCtrlHandler.
        public delegate bool HandlerRoutine(CtrlTypes CtrlType);

        // An enumerated type for the control messages
        // sent to the handler routine.
        public enum CtrlTypes
        {
            CTRL_C_EVENT = 0,
            CTRL_BREAK_EVENT,
            CTRL_CLOSE_EVENT,
            CTRL_LOGOFF_EVENT = 5,
            CTRL_SHUTDOWN_EVENT
        }

        #endregion

    }
}

http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/707e9ae1-a53f-4918-8ac4-62a1eddb3c4a/

As a last resort you can use Console.ReadLine() to stop the app from closing...

You can then do your clean up and exit the application.

Console.ReadLine()

//your clean up code here

//Exit
System.Windows.Forms.Application.Exit(0)

http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号