19

I'm writting a console application in C++.

I use SetConsoleCtrlHandler to trap close and CTRL+C button. This allows for all my threads to stop and exit properly.

One of the thread performs some saving that require some time to complete and I have some code to wait in the console crtl handle routine. MSDN specify that a box should pop up after 5 seconds for CTRL_CLOSE_EVENT, but instead my process exits.

This is annoying for debugging console application too as the process exits before you can step through and I don't know what may be the problem (I have Windows 7 64bits).

Also, strangely if my routine returns TRUE (to simply disable the close action), it still closes the application. The routine does get called, so the SetConsoleCtrlHandler was successful installed.

e.g.:

BOOL WINAPI ConsoleHandlerRoutine(DWORD dwCtrlType)
{
    if (dwCtrlType == CTRL_CLOSE_EVENT)
    {
        return TRUE;
    }

    return FALSE;
}

int _tmain(int argc, _TCHAR* argv[])
{
    BOOL ret = SetConsoleCtrlHandler(ConsoleHandlerRoutine, TRUE);

    while (true)
    {
        Sleep(1000);
    }
    return 0;
}

Any ideas?

5 Answers 5

17

It looks like you can no longer ignore close requests on Windows 7.

You do get the CTRL_CLOSE_EVENT event though, and from that moment on, you get 10 seconds to do whatever you need to do before it auto-closes. So you can either do whatever work you need to do in the handler or set a global flag.

case CTRL_CLOSE_EVENT: // CTRL-CLOSE: confirm that the user wants to exit.
                       close_flag = 1;
                       while(close_flag != 2)
                         Sleep(100);
                       return TRUE;

Fun fact: While the code in your CTRL_CLOSE_EVENT event runs, the main program keeps on running. So you'll be able to check for the flag and do a 'close_flag = 2;' somewhere. But remember, you only have 10 seconds. (So keep in mind you don't want to hang up your main program flow waiting on keyboard input for example.)

3
  • 13
    10 seconds time is only half the truth. The application will be terminated once the handler returns, so you must keep the handler running. Jan 9, 2012 at 15:22
  • 1
    Note that the handler is run in the context of an arbitrary thread (that's the reason why the programm continues running). Hence you should use some threadsafe mechanism to signal that termination is in progress (e.g. an event).
    – Max Truxa
    Oct 2, 2014 at 12:36
  • If the handler returns false and the default handler gets called, then the process exits via ExitProcess(STATUS_CONTROL_C_EXIT). If the process exits on its own before returning, it can use a different exit code to indicate a successful shutdown. Otherwise if the handler returns true or takes more than 5 seconds (not 10 seconds), the process gets terminated forcefully by the session server (csrss.exe) with the exit code STATUS_CONTROL_C_EXIT.
    – Eryk Sun
    Nov 1, 2017 at 2:00
3

I suspect that this is by-design on Windows 7 - if the user wants to quit your application, you're not allowed to tell him "No".

1
  • 2
    Agreed. The dialog that used to be displayed is gonzo. Sep 4, 2010 at 16:51
3

There is no need to wait for any flag from the main thread, the handler terminates as soon as the main thread exits (or after 10s).

BOOL WINAPI ConsoleHandler(DWORD dwType)
{
    switch(dwType) {
    case CTRL_CLOSE_EVENT:
    case CTRL_LOGOFF_EVENT:
    case CTRL_SHUTDOWN_EVENT:

      set_done();//signal the main thread to terminate

      //Returning would make the process exit!
      //We just make the handler sleep until the main thread exits,
      //or until the maximum execution time for this handler is reached.
      Sleep(10000);

      return TRUE;
    default:
      break;
    }
    return FALSE;
}
3

Xavier's comment is slightly wrong. Windows 7 allows your code in the event handler ~10 seconds. If you haven't exited the event handler in 10 seconds you are terminated. If you exit the event handler you are terminated immediately. Returning TRUE does not post a dialog. It just exits.

0

You're making this more complicated than it needs to be. I don't know exactly why your app is closing, but SetConsoleCtrlHandler(NULL, TRUE) should do what you want:

http://msdn.microsoft.com/en-us/library/ms686016(VS.85).aspx

If the HandlerRoutine parameter is NULL, a TRUE value causes the calling process to ignore CTRL+C input, and a FALSE value restores normal processing of CTRL+C input.

1
  • 4
    Still exits when I press the close button on my console. CTRL+C is not the problem as this also does the trick and works: if (dwCtrlType == CTRL_C_EVENT) return TRUE; It's the close button that is causing me problems.
    – Jeremy
    Sep 5, 2010 at 9:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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.