3

How do I handle the 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.

1
  • 2
    "That few seconds isn't enough for my app to clean up." - What kind of app is this? Maybe it is valid for you to require so much time to clean up, but perhaps the real problem is that you need it at all. Maybe you could re-examine your design a bit?
    – Ed S.
    Jul 6, 2011 at 5:08

2 Answers 2

2

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

4
  • Doesn't work. Whats weird is the app can prevent/delay the close (with and without your solution) if the user used "ctr+c". But when the user pressed the close/X button, Windows force terminates it. Jul 6, 2011 at 6:30
  • No you are right. There's a ton of stuff about this, not just for C#. What a PITA. How about this: have the executable start itself in a second process, with a different run loop (or have two executables). The first one is just a pass through and pipes to the second, but set-up the second so it doesn't die when the console is killed.
    – jamie
    Jul 6, 2011 at 6:54
  • Accepted your answer. A possible solution. But I will just switch to a GUI (less hackish). Microsoft broke the compatibility! Jul 6, 2011 at 7:17
  • 1
    Incidentally, that's what Visual Studio does. When you run it from the command line you actually invoke devenv.com, which then calls out to devenv.exe.
    – jamie
    Jul 6, 2011 at 7:20
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.