In windows console application, one can catch pressing ctrl+c
by using:
#include <stdio.h>
#include <signal.h>
void SigInt_Handler(int n_signal)
{
printf("interrupted\n");
}
int main(int n_arg_num, const char **p_arg_list)
{
signal(SIGINT, &SigInt_Handler);
getchar(); // wait for user intervention
}
This works well, except it does not work at all if the user presses the cross ×
that closes the console window. Is there any signal for that?
The reason I need this is I have this CUDA application which tends to crash the computer if closed while computing something. The code is kind of multiplatform so I'd prefer using signals rather than SetConsoleCtrlHandler
. Is there a way?