c++winapiwtl

Techniques for managing application shutdown in Win32


We have a Win32 application written using WTL (Windows Template Library), and I'm looking for patterns for exiting the application. The issue that I'm dealing with is that some of the views in the application contain resources which may take some time (measured in 1 to 2 seconds), to destroy (i.e. waiting for a thread to exit, etc)

Win32 seems to support two main lifecycle messages, WM_CLOSE and WM_DESTROY. WM_CLOSE seems to be the message you want to send windows to notify them that there is a request to close the application, and gives you the opportunity to decide on whether or not to continue to close. WM_DESTROY, is when take down is occurring and should just be cleaning up.

The question is, how would you handle a case where you want to keep the message loop running, while shutting down the application.

Example: Main window receives WM_CLOSE because user clicks on close. Let's say you don't need to prompt the user for exiting, but you then notify all of your views that a close is occuring. Let's also say that at this point, you don't want to exit the application because you need to clean up some resources, which may take a second or two, and you want the interaction to be asynchronous.

How would you notify the main window when all of the child views have completed their shutdown processes? To be more specific, is there any standard message for notifying parents that you are done cleaning up?


Solution

  • The question is, how would you handle a case where you want to keep the message loop running, while shutting down the application.

    I would have the WM_CLOSE handler display a "Please wait" message to the user, and then asynchronously initiate whatever shutdown logic is needed. Do not call DefWindowProc() or DestroyWindow() yet.

    Let the message loop run normally while the shutdown logic is doing its thing in the background.

    When all shutdown tasks are finished, dismiss the "Please wait" message, and destroy the application window, signalling the message loop to exit, as explained by the other answers.