Reputation: 5477
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:
ExitProcess
. However, this seems to do a lot of work and maybe too much?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?
The following are some interesting reads on the subject, but none of these answers this question.
Upvotes: 0
Views: 2047
Reputation: 47954
Update:
_exit() will quickly terminate the process without the unnecessary work. (Don't confuse this with exit().)
Interestingly, the implementation of _exit() ends with a call to ExitProcess, so the DLL_PROCESS_DETACH notifications are still sent, but the runtime DLL won't run static destructors in this case because _exit() sets a flag first.
Original Answer:
ExitProcess is appropriate. It may sound like a lot of work, but much of that is going to happen anyway, even if your process crashes.
The only significant, unnecessary work might be the DLL_PROCESS_DETACH notifications to the DLLs. The best thing you can do about that is to make sure your DLLs don't do much work when they receive those notifications.
Another option would be to call abort or _exit, but I suspect these are roughly equivalent to ExitProcess.
Upvotes: 2
Reputation: 36308
I agree with Adrian that ExitProcess() is usually the most appropriate choice.
However, if you absolutely must (for example) prevent the C++ destructors from running, you can always resort to the nuclear option:
TerminateProcess(GetCurrentProcess());
Upvotes: 1