C ++: continue execution after SIGINT

Well, I am writing a program that conducts a rather difficult analysis, and I would like to quickly stop it.

I added signal(SIGINT, terminate);at the beginning of the main and specific end, for example:

void terminate(int param){
   cout << endl << endl << "Exit [N]ow, or [A]fter this url?" << endl;
   std::string answer;
   cin >> answer;
   if(answer[0] == 'n' || answer[0] == 'N'){
      terminateParser();
      exit(1);
   }else if(answer[0] == 'a' || answer[0] == 'A'){
      quitAfterUrl = true;
   }
}

On linux, this worked the way I expected it to wait for user input. But when I try to do the same in windows, it still shows the message and exits.

Is there any way to stop SIGINT immediately after closing the program?

Update:

when i tried

BOOL WINAPI handler(DWORD dwCtrlType)
{
  if (CTRL_C_EVENT == dwCtrlType)
  {
    // ask the user
  }
  return FALSE;
}

as suggested by Gregory, the program was still unceremoniously exiting without stopping user input.

Update 2: I'm not quite sure I did this, but now the code works. Thank you all for your help.

+3
1

MSDN:

SIGINT Win32, Windows 98/Me Windows NT/2000/XP. CTRL + C, Win32 , . , , UNIX, , .

, Windows.

BOOL WINAPI handler(DWORD dwCtrlType)
{
  if (CTRL_C_EVENT == dwCtrlType)
  {
    // ask the user
  }
  return FALSE;
}

main

SetConsoleCtrlHandler(handler, TRUE);
+8

Source: https://habr.com/ru/post/1727830/


All Articles