What is the proper way to 'quit' an application after final clean-up tasks have been done, in response to the WM_ENDSESSION
message?
Once my application, a service-like executable responding to external input, receives this message and wParam == TRUE
it writes the end of the log and flushes it. After this point, I actually want my application to stop, completely. I don't want any further code to run, because clean-up will be done by Windows and any servicing done by my application after this message is likely to be rudely interrupted by process termination.
I want my application to do only the absolutely necessary amount of work and after that stop so as to not slow down Windows shutdown. Some possibilities I can think of are:
- One option I can think of is to call
ExitProcess
. However, this seems to do a lot of work and maybe too much? - Another option is to set a flag after receiving
WM_ENDSESSION
and have strategically placed checks for this flag in my code so that the program does nothing. The obvious disadvantage is that this complicates the code unnecessarily.
There might be other options and I want to know: which is the proper way?
Interesting reads
The following are some interesting reads on the subject, but none of these answers this question.
- What's the proper response to WM_ENDSESSION with ENDSESSION_CLOSEAPP and wParam == FALSE?
- Should I process WM_ENDSESSION, WM_QUERYENDSESSION, both or neither?
- Problem with WM_ENDSESSION message
- C++: cleanup actions in response to Windows logoff
- The Old New Thing: Once you return from the WM_ENDSESSION message, your process can be terminated at any time
- The Old New Thing: When DLL_PROCESS_DETACH tells you that the process is exiting, your best bet is just to return without doing anything
_exit(1);
– Hans PassantWM_QUERYENDSESSION
handler, since there is no guarantee that aWM_ENDSESSION
will follow. You can, however, flush any buffers, that need to be written out to disk, to reduce the processing time in yourWM_ENDSESSION
handler to its minimum. – IInspectableWM_QUERYENDSESSION
, you are guaranteed to receiveWM_ENDSESSION
. What is NOT guaranteed is that the session will actually end if you receiveWM_ENDSESSION
. This is stated in the documentation: "When an application returns TRUE for this message, it receives the WM_ENDSESSION message, regardless of how the other applications respond to the WM_QUERYENDSESSION message. Each application should return TRUE or FALSE immediately upon receiving this message, and defer any cleanup operations until it receives the WM_ENDSESSION message." – Remy Lebeau