@qtYoda
There is a way to be notified when the user closes the console or hits Ctrl+C. I have tried it and it works; you might want to try it to see if it also works when the user shuts down Windows without closing the console first (it should according to MSDN). I did the following:
1. upon initializing the application, call SetConsoleCtrlHandler() to register a handler that will get called when the console is closed or Ctrl+C is hit;
2. in the handler, forward the notification to Qt's event loop by using e.g. QCoreApplication::postEvent() with a dedicated custom event, then return TRUE;
3. do the heavy work upon receiving the custom event, then exit using e.g. QCoreApplication::quit().
Note that it is simpler than the equivalent in Unix, where you cannot do much in a signal handler. There are a number of solutions to that issue, notably using a pipe, or running sigwait() in a dedicated thread, or using the Linux-specific signalfd() system call.
Bookmarks