C++: Continue execution after SIGINT

前端 未结 1 390
孤城傲影
孤城傲影 2021-01-21 06:13

Okay, I am writing a program that is doing some pretty heavy analysis and I would like to be able to stop it quickly.

I added signal(SIGINT, terminate); to

相关标签:
1条回答
  • 2021-01-21 06:20

    From MSDN:

    Note SIGINT is not supported for any Win32 application, including Windows 98/Me and Windows NT/2000/XP. When a CTRL+C interrupt occurs, Win32 operating systems generate a new thread to specifically handle that interrupt. This can cause a single-thread application such as UNIX, to become multithreaded, resulting in unexpected behavior.

    Which means you will have to use preprocessing directive and implement a Windows specific solution.

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

    And at the beginning of main you do

    SetConsoleCtrlHandler(handler, TRUE);
    
    0 讨论(0)
提交回复
热议问题