Preventing MSYS 'bash' from killing processes that trap ^C

前端 未结 6 1286
自闭症患者
自闭症患者 2021-02-01 14:56

I have a console-mode Windows application (ported from Unix) that was originally designed to do a clean exit when it received ^C (Unix SIGINT). A clean

6条回答
  •  一生所求
    2021-02-01 15:35

    Arg - 5 minute edit on comment. Here's what I wanted to write:

    As a workaround, instead of trying to trap the CTRL-C event which is also being propagated to the shell I'd propose turning off the ENABLED_PROCESSED_INPUT on stdin so that CTRL-C is reported as a keyboard input instead of as a signal:

    DWORD mode;
    HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
    GetConsoleMode(hstdin, &mode);
    SetConsoleMode(hstdin, mode & ~ENABLE_PROCESSED_INPUT); /* disable CTRL-C processing as a signal */
    

    You could then process keyboard input in your main thread while the rest of the program does its thing in a separate thread and set an event to cleanup when CTRL-C is received.

提交回复
热议问题