SetConsoleCtrlHandler routine issue

后端 未结 5 873
逝去的感伤
逝去的感伤 2021-02-18 21:43

I\'m writting a console application in C++.

I use SetConsoleCtrlHandler to trap close and CTRL+C button. This allows for all my threads to stop and exit properly.

<
5条回答
  •  滥情空心
    2021-02-18 22:12

    There is no need to wait for any flag from the main thread, the handler terminates as soon as the main thread exits (or after 10s).

    BOOL WINAPI ConsoleHandler(DWORD dwType)
    {
        switch(dwType) {
        case CTRL_CLOSE_EVENT:
        case CTRL_LOGOFF_EVENT:
        case CTRL_SHUTDOWN_EVENT:
    
          set_done();//signal the main thread to terminate
    
          //Returning would make the process exit!
          //We just make the handler sleep until the main thread exits,
          //or until the maximum execution time for this handler is reached.
          Sleep(10000);
    
          return TRUE;
        default:
          break;
        }
        return FALSE;
    }
    

提交回复
热议问题