[英]wxWidgets C++ console application: how to capture INT/TERM signals?
I have a wxWidgets console application, and I need to handle SIGINT / SIGTERM signals to shutdown program properly. 我有一个wxWidgets控制台应用程序,我需要处理SIGINT / SIGTERM信号才能正确关闭程序。
I've tried "classic" signals handling, but it does not works: 我已经尝试过“经典”信号处理,但是它不起作用:
#include <signal.h>
bool MainApp::OnInit()
{ // terminate gracefully:
// term threads, wait them for terminate, etc.
}
static void OnSignal(int sig)
{ // captured signal:
cout<<"Terminating...\r\n";
wxGetApp().OnExit();
}
bool MainApp::OnInit()
{
// init app here, create working threads, etc.
// set own handler:
cout<<"Signal set handler result: "<<signal(SIGINT,OnSignal);// returns 0
return true;// app inited successfully
}
When I send SIGINT or SIGTERM signals (using integrated CodeLite's terminal for debugging), nothing happens. 当我发送SIGINT或SIGTERM信号(使用集成的CodeLite终端进行调试)时,什么都没有发生。
Looks like wxWidgets still does not have signal processing - at least, I've found nothing in documentation. 看起来wxWidgets仍然没有信号处理-至少,我在文档中什么都没找到。
The question is: how to capture INT/TERM signals? 问题是: 如何捕获INT / TERM信号? (At least in Linux, but cross-platform solution is of course better) .
(至少在Linux中,但是跨平台解决方案当然更好) 。
You can't use cout
nor exit the application from a signal handler, so while I don't know why it isn't called at all for you, things would only be worse, not better, if it was. 您不能使用
cout
也不能从信号处理程序中退出应用程序,因此尽管我不知道为什么根本没有为您调用它,但情况只会变得更糟,而不是更好。
wxWidgets has private (ie undocumented, liable to change) wxApp::SetSignalHandler()
in Unix ports which implements proper signal handling, with the handler itself just waking up the event loop and telling it to wait for the signals. wxWidgets在Unix端口中具有私有的(即未记录的,易于更改)
wxApp::SetSignalHandler()
实现正确的信号处理,而处理程序本身只是唤醒事件循环并告诉它等待信号。 If you do anything other than just setting a flag in your signal handler, this is what you should do as well. 如果您不只是在信号处理程序中设置标志,还要做其他事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.