本文介绍了SetConsoleCtrlHandler等效于Unix / Linux C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道等同于Windows 功能? (特别是对于wxWidgets应用程序。)

Does anybody know an equivalent of the Windows SetConsoleCtrlHandler function for Linux/UNIX OS? (Specifically, for a wxWidgets application.)

推荐答案

您可以使用。

这里有一个例子:

#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);
}

如注释中所述,避免一些陷阱, code> signal(),应该是首选。

As mentioned in the comments, sigaction() avoids some pitfalls that can be common with signal(), and should be preferred..

这篇关于SetConsoleCtrlHandler等效于Unix / Linux C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 04:59