Here is a simple application that handled CTRL+C signal booth on linux and windows:
下面是一个简单的应用程序,可以在linux和windows上处理CTRL+C信号亭:
#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 following output:
在linux (Debian Squeeze)上编译并运行后,得到如下输出:
/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 that's what I've expected (SigIntHandler method runs on main thread). But when I compile and execute same code on Windows 7, I get this:
这就是我所期望的(SigIntHandler方法在主线程上运行)。但是当我在Windows 7上编译并执行相同的代码时,我得到了:
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 SigIntHandler method is executed in different thread then main ... And that creates me many problems. So my question is - is it possible to force SigIntHandler to run in main thread on windows ? Am I maybe catching siging wrong ?
如您所见,SigIntHandler方法在不同的线程中执行,然后是main…这给我带来了很多问题。我的问题是,是否有可能强制SigIntHandler在windows的主线程中运行?我是不是记错了?
Thanks !!
谢谢! !
2 个解决方案
#1
10
From MSDN topic HandlerRoutine:
从MSDN HandlerRoutine话题:
A HandlerRoutine function is an application-defined function used with the SetConsoleCtrlHandler function. A console process uses this function to handle control signals received by the process. When the signal is received, the system creates a new thread in the process to execute the function.
HandlerRoutine函数是一个应用程序定义的函数,用于setcomfort ectrlhandler函数。控制台进程使用此函数来处理进程接收的控制信号。当接收到信号时,系统在进程中创建一个新线程来执行函数。
So, the answer is: this is impossible.
所以,答案是:这是不可能的。
#2
5
Read the following link for an interesting take on this: http://blogs.msdn.com/b/oldnewthing/archive/2008/07/28/8781423.aspx
请阅读下面的链接以获得有趣的体验:http://blogs.msdn.com/b/oldnewthing/archive/2008/07/28/8781423.aspx
#1
10
From MSDN topic HandlerRoutine:
从MSDN HandlerRoutine话题:
A HandlerRoutine function is an application-defined function used with the SetConsoleCtrlHandler function. A console process uses this function to handle control signals received by the process. When the signal is received, the system creates a new thread in the process to execute the function.
HandlerRoutine函数是一个应用程序定义的函数,用于setcomfort ectrlhandler函数。控制台进程使用此函数来处理进程接收的控制信号。当接收到信号时,系统在进程中创建一个新线程来执行函数。
So, the answer is: this is impossible.
所以,答案是:这是不可能的。
#2
5
Read the following link for an interesting take on this: http://blogs.msdn.com/b/oldnewthing/archive/2008/07/28/8781423.aspx
请阅读下面的链接以获得有趣的体验:http://blogs.msdn.com/b/oldnewthing/archive/2008/07/28/8781423.aspx