Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
i made a sample to check what happen when i press ctrl+C in windows console application:

C++
bool    TerminationFlag=true;
int main()
{
g_hTerminateEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
::SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
while(1)
    {
if(TerminationFlag==false)
        {
            break;
        }
}
return 0;
}

BOOL WINAPI ConsoleCtrlHandler(DWORD dwCtrlType)
{

    if (dwCtrlType == CTRL_C_EVENT ||
        dwCtrlType == CTRL_BREAK_EVENT ||
        dwCtrlType == CTRL_CLOSE_EVENT)
    {
    TerminationFlag=false;
::SetEvent(g_hTerminateEvent);
return TRUE;
    }
return FALSE;
}


SQL
i tested the code by running it using start debugging option in visual studio when i press ctrl+c i get the following message

First-chance exception at 0x7c87647d when i press on continue option my code comes to the line TerminationFlag=false;

even though i have handled ctrl+c in control handler.Can you please tell me whats the problem?
Posted

1 solution

It is right in the MSDN[^]. See the Remarks section close to the end.

If a console process is being debugged and CTRL+C signals have not been disabled, the system generates a DBG_CONTROL_C exception. This exception is raised only for the benefit of the debugger, and an application should never use an exception handler to deal with it. If the debugger handles the exception, an application will not notice the CTRL+C, with one exception: alertable waits will terminate. If the debugger passes the exception on unhandled, CTRL+C is passed to the console process and treated as a signal, as previously discussed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900