Windows doesn't close windows when a user logs off; that's your call

Raymond Chen

Commenter Peter Kankowski asks why Windows doesn’t send WM_CLOSE and WM_DESTROY messages when the user logs off. That’s what WM_ENDSESSION is for. To tell the program that the Windows session is ending, and that you should get done whatever last things you want to get done before the world comes to an end. Windows doesn’t send the WM_CLOSE message because sending WM_CLOSE becomes complicated once there is more than one window. What is the correct order for sending WM_CLOSE when there are multiple windows? Besides, many programs don’t respond to WM_CLOSE by closing. Instant messenger programs typically treat WM_CLOSE to mean “hide” rather than “close”. Notepad displays an unsaved data warning when you close the window. And of course, you can’t close a disabled window. As we saw with Notepad, sending a WM_CLOSE would just repeat actions that were taken during WM_ENDSESSION, which is particularly bad because the time for user interaction is over. WM_QUERYENDSESSION is the time to say your last good-byes. If Windows sent the WM_CLOSE message during logoff, Notepad would display a “You have unsaved changes. Do you want to save this file?” message after the user already said, “Go ahead and exit without saving,” when Notepad handled the WM_QUERYENDSESSION message. “Stupid computer. Why is it asking me this question again?” Even worse, what if the user says “Cancel”? It’s too late; shutdown has begun. Your chance to cancel it was back when you got the WM_QUERYENDSESSION message. Asking for the WM_DESTROY message is even weirder, since that message is not sent explicitly but is rather generated when DestroyWindow is called. You can’t just send it outside of a destroy sequence. Besides, why spend your time closing windows when the session is about to go away anyway? Ooh, let me clean up this and destroy that, I know you asked to shut down, but this’ll just take a few seconds. It’s like taking the time to steam-clean the rugs before you demolish the building. Wasted effort.

Now, you might respond, “Yeah, sure, these are problems, but we should just require programs to address them, such as not putting up additional UI if they get a WM_CLOSE after a WM_ENDSESSION.” Of course, this argument contradicts the rationale for the original question, since the question was inspired by programs that didn’t handle the WM_ENDSESSION correctly in the first place! “We have a problem with programs that don’t handle the WM_ENDSESSION message correctly. Solution: Make WM_ENDSESSION more complicated.”

0 comments

Discussion is closed.

Feedback usabilla icon