且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++:在SIGINT后继续执行

更新时间:2022-03-14 05:44:27

从MSDN:


注意 SIGINT不适用于任何Win32应用程序,包括Windows 98 / Me和Windows NT / 2000 / XP。当发生CTRL + C中断时,Win32操作系统会生成一个新的线程来专门处理该中断。这可能会导致单线程应用程序(如UNIX)变为多线程,导致意外的行为。

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.

这意味着您必须使用预处理指令并实施 Windows特定解决方案

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

并在 main 你做

SetConsoleCtrlHandler(handler, TRUE);