Using SetConsoleCtrlHandler



Hi,

I've a console single threaded application and I'm trying to catch a Ctrl+C. No
matter if I use SetConsoleCtrlHandler or a signal handler, my code to handle
this gets called in another thread. Is there a way to have the handler called
from the main thread?

In the code below, simply comment the call to `signal' or to
`SetConsoleCtrlHandler' to observe the similar behavior. On Unix, using
`signal', it is called from the same thread.

Thanks for any highlight,
Manu

PS: this is shown by the code:

#include <windows.h>
#include <stdio.h>
#include <signal.h>

BOOL CtrlHandler( DWORD fdwCtrlType )
{
switch( fdwCtrlType ) {
case CTRL_C_EVENT:
printf( "Ctrl-C event\n\n" );
return TRUE;
default:
return FALSE;
}
}

void handler (int sig) {
printf ("From Signal\n");
signal (SIGINT, handler);
}

void main( void )
{
signal (SIGINT, handler);
//SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE );

printf("Use Ctrl+C to see what is going on.\n" );
while( 1 ){ }
}
.



Relevant Pages

  • Re: Using SetConsoleCtrlHandler
    ... My solution for a similar situation was to redirect the exception to the ... void handler { ... signal (SIGINT, handler); ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Using SetConsoleCtrlHandler
    ... ConsoleCtrlHandler and detect in your main thread? ... void handler { ... signal (SIGINT, handler); ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Using SetConsoleCtrlHandler
    ... a single thread Emmanuel (i.e. similar to UNIX signals, ... void handler { ... signal (SIGINT, handler); ...
    (microsoft.public.win32.programmer.kernel)
  • Re: SetConsoleCtrlHandler called when accessing ODBC/OLEDB datasource
    ... > I have a console mode application which behave badly when accessing a DBF ... After hours of searching, I found that TAdoQuery ... > Any idea why SetConsoleCtrlHandler is called and how to avoid this? ... SetConsoleCtrlHandler to install his own handler, ...
    (borland.public.delphi.database.ado)
  • Re: Aplication crash on Ctrl+C in console application
    ... You are better off asking this in Win32 ng as well. ... SetConsoleCtrlHandler to handle CTRL+C events. ... breakpoint inside this routine. ... It does not transfer control to my handler routine. ...
    (microsoft.public.vc.mfc)