MessageBoxTimeout

Before reading this article you may know what is Microsoft foundation classes and Visual C++.I am listing here few of its features.

When MFC was introduced, Microsoft extended the C++ syntax with a series of macros for management of Windows messages, exceptions, run time type identification, and dynamic class instantiation. The macros for Windows messages were intended to reduce memory required by avoiding gratuitous vtable use and provide a more concrete structure for various Visual C++-supplied tools to edit and manipulate code without parsing the full language.

The message-handling macros replaced the virtual function mechanism provided by C++. Because some versions of the macros defeated the type checking done by the compiler, their use has been a potential source of bugs for users of MFC.The macros which implemented serialization, exception support, and dynamic runtime types were less problematic, and predated availability of standards-based language extensions by a number of years.Provides an object-oriented programming model to the Windows APIs .

C++ wrapper types for many common Windows resource-related data types that provide automatic closure of handles when the objects creating them go out of scope. Provides a Document/View framework for creating Model-View-Controller-based architectures. Provides utility classes such as CString and collection classes, which are usable even by console applications and many more features are there.

We will see in the following code how a message box which is timed works.

This TimedMessageBox function acts just like the MessageBox function, except that if the user doesn't respond within dwTimeout milliseconds, we return -1. The limitation is that only one timed message box can be active at a time. If your program is single-threaded, this is not a serious limitation, but if your program is multi-threaded, this will be a problem.

Let us see how it works

The global static variable s_fTimedOut tells us whether we generated a fake WM_QUIT message as a result of a timeout. When the MessageBox function returns, and we indeed timed out, we use the Message function to remove the fake WM_QUIT message from the queue before returning.

Note that we remove the WM_QUIT message only if we were the ones who generated it. In this way, WM_QUIT messages generated by other parts of the program remain in the queue for processing by the main message loop. when we decide that the timeout has occurred, we re-enable the original owner window before we cause the message box to bail out of its message loop by posting a quit message.