Windows console application - signal for closing event

前端 未结 1 528
礼貌的吻别
礼貌的吻别 2020-12-19 14:23

In windows console application, one can catch pressing ctrl+c by using:

#include 
#incl         


        
相关标签:
1条回答
  • 2020-12-19 14:52

    The correct one is SIGBREAK, you can try it with:

    #include <stdio.h>
    #include <signal.h>
    
    void SigInt_Handler(int n_signal)
    {
        printf("interrupted\n");
        exit(1);
    }
    
    void SigBreak_Handler(int n_signal)
    {
        printf("closed\n");
        exit(2);
    }
    
    int main(int n_arg_num, const char **p_arg_list)
    {
        signal(SIGINT, &SigInt_Handler);
        signal(SIGBREAK, &SigBreak_Handler);
        getchar(); // wait for user intervention
        return 0;
    }
    

    Upon closing the console window, the program will print "closed" and will return 2.

    0 讨论(0)
提交回复
热议问题