The console control handler routine should check the received event signal, and only trigger for appropriate events such as Ctrl-C/close, etc. https://learn.microsoft.com/en-us/windows/console/handlerroutine