C:在SIGINT之后继续执行
作者:互联网
好的,我正在编写一个程序正在进行一些非常繁重的分析,我希望能够快速停止它.
我添加了信号(SIGINT,终止);到主要和定义终止的开头像:
void terminate(int param){
cout << endl << endl << "Exit [N]ow, or [A]fter this url?" << endl;
std::string answer;
cin >> answer;
if(answer[0] == 'n' || answer[0] == 'N'){
terminateParser();
exit(1);
}else if(answer[0] == 'a' || answer[0] == 'A'){
quitAfterUrl = true;
}
}
在linux中,这正如我所期望的那样,即它等待用户输入.但是,当我尝试在Windows中执行相同操作时,它会显示消息并退出.
有没有办法阻止SIGINT立即关闭程序?
更新:
当我试着
BOOL WINAPI handler(DWORD dwCtrlType)
{
if (CTRL_C_EVENT == dwCtrlType)
{
// ask the user
}
return FALSE;
}
正如Gregory建议的那样,该程序仍然毫不客气地退出而没有停止用户输入.
更新2:
我不确定它做了什么,但代码现在正在运行.谢谢大家的帮助.
解决方法:
Note SIGINT is not supported for any Win32 application, including Windows 98/Me and Windows NT/2000/XP. When a CTRL+C interrupt occurs, Win32 operating systems generate a new thread to specifically handle that interrupt. This can cause a single-thread application such as UNIX, to become multithreaded, resulting in unexpected behavior.
这意味着您必须使用预处理指令并实现Windows specific solution.
BOOL WINAPI handler(DWORD dwCtrlType)
{
if (CTRL_C_EVENT == dwCtrlType)
{
// ask the user
}
return FALSE;
}
在主要的开始你做
SetConsoleCtrlHandler(handler, TRUE);
标签:c,windows,copy-paste,signals,sigint 来源: https://codeday.me/bug/20190726/1547032.html