I am trying to gracefully exit the console application when the close button is pressed.
bool done = false;
BOOL ctrl_handler(DWORD event)
{
if (event == CTRL_CLOSE_EVENT) {
done = true;
return TRUE;
}
return FALSE;
}
int main()
{
SetConsoleCtrlHandler((PHANDLER_ROUTINE)(ctrl_handler), TRUE);
while (!done)
do_stuff();
cleanup();
cout << "bye";
}
My handler is called correctly, but the main thread does not resume after that, so the buy never happens. If instead I hold CTRL_C_EVENT and press ^ C in the console, then the main thread will continue and exit gracefully. Is there a way to allow the main thread to gracefully exit the console?
I also tried using std :: signal, but this is the same. Works for ^ C, but not for closing a window.
tukra source
share