2012-12-11 104 views

回答

2

您可以使用'signal()' function

下面是一個例子:

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

正如評論所說,sigaction()避免一些陷阱,可以是常見的有signal(),並且應首選..

+3

或者[sigaction](http://pubs.opengroup.org/onlinepubs/009695399/functions/sigaction.html)。 –

+0

inb4:http://stackoverflow.com/questions/231912/what-is-the-difference-between-sigaction-and-signal –

+0

@LightnessRacesinOrbit感謝您的參考,非常翔實。這是通常一次不會修改的事情之一,所以最後一次我做了這件事,我沒有聽說過'sigaction'。 – Chad