A customer discovered that their application's shutdown code
To address the problem, they moved the bulk of their shutdown code
WM_ENDSESSION message handler.
my earlier discussion of the
and wondered if they were doing the right thing.
Yes, it's okay to do shutdown activities in response to the
wParam is nonzero,
indicating that the session really is ending.
wParam is zero,
then it means that the session is not ending,
so you had better not destroy anything you still need.
Recall the shutdown sequence:
First, the application receives a
Here is the traditional point at which
you can display a prompt to ask the user
whether they want to save their unsaved changes.¹
Normally, you return
but if the user hits Cancel or otherwise indicates
that they don't want to shut down after all,
then you return
If you returned
then you will eventually receive a
wParam indicates whether the session
really is ending.
(The session might not actually be ending if another
or if the user canceled shutdown from the UI.)
The customer shared some of their code,
and I noticed that they were destroying a window
handler, which is suspicious for two reasons:
FALSE, the application will continue to run, but it lost one of its windows!
TRUE, then it's okay to destroy things, but remember that you are running under a time constraint, and the building is being demolished, so you probably shouldn't be wasting time sweeping the floor and emptying the trash cans.
What you could do is to kick off a background thread
to prepare for shutdown when you receive the
For example, you might start
an autosave operation.
Whatever you do, make sure that it's okay for the operation
to occur even if the shutdown is subsequently canceled.
When you get the
you wait until that background operation completes
before telling the system,
"I'm good; you can shut down now."
Opportunistically starting the operation when you get the
means that you can respond more quickly to the
¹ In practice, displaying a prompt is usually not a good idea because if you don't respond to the message after a few seconds, the system will shut down without you.