Emmanuel Stapf [ES]
2007-06-06 01:18:06 UTC
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 ){ }
}
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 ){ }
}