Reputation: 182
I am currently writing on an background application. It has no window or console running and is also no service. I am using the /SUBSYSTEM:WINDOWS
inside my CMake.
So I know there are the WM_CLOSE
and the CTRL_CLOSE_EVENT
and I could spawn an invisible window to wait for the WM_CLOSE
.
My question now is, what happens to the application if it doesn't do anything of this and windows wants to shut down?
Does it simply kill my application "SIGKILL" style or am I missing something here? I want to know of the shutdown to clean some stuff up, mainly handles
Upvotes: 1
Views: 245
Reputation: 10995
On logoff / shutdown all processes associated with that session will be terminated.
(equivalent to TerminateProcess()
, i.e. the windows SIGKILL
equivalent)
This is described in Logging off / Shutting down on msdn.
There are a few ways you can register a handler to get a notification before your process gets terminated, depeding on the type of your application:
SetConsoleCtrlHandler()
WM_QUERYENDSESSION
/ WM_ENDSESSION
messages
WM_CLOSE
mesageSetConsoleCtrlHandler()
, due to the handler not being called on logoff / shutdown:SetConsoleCtrlHandler()
Remarks Section:
If a console application loads the gdi32.dll or user32.dll library, the HandlerRoutine function that you specify when you call SetConsoleCtrlHandler does not get called for the CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT events.
RegisterServiceCtrlHandlerEx()
Note that you can't delay logoff / shutdown forever with those notifications; after +/- 5 seconds (non-contractual) Windows will show a list of applications that are preventing shutdown, asking the user if he wants to forcefully close them. (this one)
If the user doesn't choose either option within a few seconds then Windows will auto-pick "Shutdown anyway". (resulting in termination of your process)
Good read about this topic: If one program blocks shutdown, then all programs block shutdown by Raymond Chen
Note that you don't have to free allocated memory, close handles, etc... before your process gets terminated.
See Terminating a Process for more details.
So the only thing you would need to worry about is application-specific state. (e.g. if you need to persist in-memory state to the disk)
I think this quote from Raymond Chen about process shutdown really gets the point across:
The building is being demolished.
Don’t bother sweeping the floor and emptying the trash cans and erasing the whiteboards.
And don’t line up at the exit to the building so everybody can move their in/out magnet to out.
All you’re doing is making the demolition team wait for you to finish these pointless housecleaning tasks.
i.e. don't bother freeing allocated memory, closing handles, etc... if you know your process is about to be terminated anyway; all you're doing is delaying shutdown / logoff for no reason.
Good reads about this topic:
Upvotes: 1