Does anybody know an equivalent of the Windows SetConsoleCtrlHandler
function for Linux/UNIX OS? (Specifically, for a wxWidgets application.)
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can use the 'signal()' function.
Here's an example:
#include <signal.h>
bool stop = false;
void app_stopped(int sig)
{
// function called when signal is received.
stop = true;
}
int main()
{
// app_stopped will be called when SIGINT (Ctrl+C) is received.
signal(SIGINT, app_stopped);
while(false == stop)
Sleep(10);
}
As mentioned in the comments, sigaction()
avoids some pitfalls that can be common with signal()
, and should be preferred..