The Message Loop Is Your Friend
Messages in Windows
The Windows operating system has always been message-based. If you've written a message loop for a Windows application, you probably know that there are thousands of messages flying over the system every second. Technologies such as DDE, OLE, COM, and so on have their roots in the Windows messaging system.
Windows messaging allows the operating system to make calls to functions in your programs. Some common examples of Windows messages used in programs are WM_PAINT, WM_COMMAND, and WM_CLOSE.
As an example, if your application receives a WM_CLOSE message from the application (this usually happens when Windows shuts down), you can process this message to tell the OS to wait until you can gracefully shut down your application by doing your internal housekeeping, and then the OS can shut down without having left open files, handles, etc. in your application.