Does Windows handle CTRL + C in different threads?

Here is a simple application that handled the CTRL + C stand on linux and windows:

#include <QtCore/QCoreApplication> #include <QDebug> #include <QThread> void SigIntHandler() { qDebug()<<"SigInt ThreadID: "<<QThread::currentThreadId(); qApp->quit(); } #ifdef __linux__ #include <signal.h> void unix_handler(int s) { //svakako je SIGINT, ali da ne javlja warning da se s ne koristi if (s==SIGINT) SigIntHandler(); } #else #include <windows.h> BOOL WINAPI WinHandler(DWORD CEvent) { switch(CEvent) { case CTRL_C_EVENT: SigIntHandler(); break; } return TRUE; } #endif int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //kod za hvatanje CTRL+C - unix i windows #ifdef __linux__ signal(SIGINT, &unix_handler); #else SetConsoleCtrlHandler((PHANDLER_ROUTINE)WinHandler, TRUE); #endif qDebug()<<"Main ThreadID: "<<QThread::currentThreadId(); return a.exec(); } 

After compiling and running it on linux (Debian Squeeze), I get the following output:

 /Test-build-desktop$ ./Test Main ThreadID: 140105475446560 ^CSigInt ThreadID: 140105475446560 /Test-build-desktop$ ./Test Main ThreadID: 140369579480864 ^CSigInt ThreadID: 140369579480864 /Test-build-desktop$ ./Test Main ThreadID: 140571925509920 ^CSigInt ThreadID: 140571925509920 

And this is what I expected (the SigIntHandler method works in the main thread). But when I compile and execute the same code on Windows 7, I get the following:

 d:\Test-build-desktop\debug>Test.exe Main ThreadID: 0x5a8 SigInt ThreadID: 0x768 d:\Test-build-desktop\debug>Test.exe Main ThreadID: 0x588 SigInt ThreadID: 0x1434 d:\Test-build-desktop\debug>Test.exe Main ThreadID: 0x1170 SigInt ThreadID: 0xc38 

As you can see, here the SigIntHandler method is executed in another thread, then main ... And this creates a lot of problems for me. So my question is: is it possible to get SigIntHandler to start in the main thread in windows? Maybe I get it wrong?

Thanks!

+7
source share
2 answers

From the MSDN HandlerRoutine topic:

The HandlerRoutine function is an application-defined function used in the SetConsoleCtrlHandler function. The console process uses this function to process control signals received by the process. When a signal is received, the system creates a new thread in the process to execute the function.

So the answer is this: it is impossible.

+10
source
+5
source

All Articles