This article was previously published under Q97858
This article has been archived. It is offered "as is" and will no longer be updated.
An exception is raised for CTRL+C only if the process is being debugged.The purpose is to make it convenient for the debugger to catch CTRL+C inconsole applications. For the purposes of this article, the debugger isassumed to be WinDbg.
When the console server detects a CTRL+C, it uses CreateRemoteThread() tocreate a thread in the client process to handle the event. This new threadthen raises an exception IF AND ONLY IF the process is being debugged. Atthis point, the debugger either handles the exception or it continues theexception unhandled.
The "gh" command marks the exception as having been handled and continuesthe execution. The application does not notice the CTRL+C, with oneexception: CTRL+C causes alertable waits to terminate. This is mostnoticeable when executing:
It is not possible to get the debugger to stop the wait from terminating.
The "gn" command marks an exception as unhandled and continues theexecution. The handler list for the application is searched, as documentedfor SetConsoleCtrlHandler(). The handler is executed in the thread createdby the console server.
After the exception is handled, the thread created to handle the eventterminates. The debugger will not continue to execute the application if GoOn Thread Termination is not enabled (from the Options menu, choose Debug,and select the Go On Thread Termination check box). The thread and processstatus indicate that the application is stopped at a debug event. As soonas the debugger is given a go command, the dead thread disappears and theapplication continues execution.
There are three cases where CTRL+C doesn't cause the program to stopexecuting (instead it causes a "page down"):
When CTRL+C is already being handled.
When the debugger is in the foreground and a source window has the focus (both must be true).
When the CTRL+C exception is disabled (through the Debugger Exceptions dialog box).