Does anybody know an equivalent of the Windows SetConsoleCtrlHandler
function for Linux/UNIX OS? (Specifically, for a wxWidgets application.)
Asked
Active
Viewed 2,021 times
1

James M
- 18,506
- 3
- 48
- 56

KuperMuper
- 49
- 2
- 6
-
What research have you performed so far? – Lightness Races in Orbit Dec 11 '12 at 18:09
-
From what I can tell, POSIX signals are the closest match. – Lightness Races in Orbit Dec 11 '12 at 18:10
1 Answers
4
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..

Chad
- 18,706
- 4
- 46
- 63
-
3Or rather [sigaction](http://pubs.opengroup.org/onlinepubs/009695399/functions/sigaction.html). – Hristo Iliev Dec 11 '12 at 18:10
-
inb4: http://stackoverflow.com/questions/231912/what-is-the-difference-between-sigaction-and-signal – Lightness Races in Orbit Dec 11 '12 at 18:12
-
@LightnessRacesinOrbit thanks for the reference, very informative. This is one of those things that generally once in place tend to not be modified, so the last time I did this I hadn't heard of `sigaction`. – Chad Dec 11 '12 at 18:19