Message loop in Microsoft Windows
The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows.[src] Windows programs that have a GUI are event-driven.[src] Windows maintains an individual message queue for each thread that has created a window.[src] Usually only the first thread creates windows.[src] Windows places messages into that queue whenever mouse activity occurs on that thread's window, whenever keyboard activity occurs while that window has focus, and at other times.[src] A process can also add messages to its own queue.[src] To accept user input, and for other reasons, each thread with a window must continuously retrieve messages from its queue, and act on them.[src] A programmer makes the process do that by writing a loop that calls GetMessage (which blocks for a message and retrieves it), and then calls DispatchMessage (which dispatches the message), and repeats indefinitely.[src] This is the message loop.[src] There usually is a message loop in the main program, which runs on the main thread, and additional message loop in each created modal dialog.[src] Messages for every window of the process pass through its message queue, and are handled by its message loop.[src] A message loop is one kind of event loop.…[src]
The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows.[src] Windows programs that have a GUI are event-driven.[src] Windows maintains an individual message queue for each thread that has created a window.[src] Usually only the first thread creates windows.[src] Windows places messages into that queue whenever mouse activity occurs on that thread's window, whenever keyboard activity occurs while that window has focus, and at other times.[src] A process can also add messages to its own queue.[src] To accept user input, and for other reasons, each thread with a window must continuously retrieve messages from its queue, and act on them.[src] A programmer makes the process do that by writing a loop that calls GetMessage (which blocks for a message and retrieves it), and then calls DispatchMessage (which dispatches the message), and repeats indefinitely.[src] This is the message loop.[src] There usually is a message loop in the main program, which runs on the main thread, and additional message loop in each created modal dialog.[src] Messages for every window of the process pass through its message queue, and are handled by its message loop.[src] A message loop is one kind of event loop.[src] A basic message loop appears as follows: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; int bRet; while (1) { bRet = GetMessage(&msg, NULL, 0, 0); if (bRet == -1) // If bRet is -1, GetMessage has failed.[src] { // Handle or log the error; possibly exit.[src] // ...[src] } else if (bRet != 0) // If bRet is not 0 or -1, the message must be processed.[src] { TranslateMessage(&msg); DispatchMessage(&msg); } else // If bRet is 0, the message loop should exit.[src] { break; } } return msg.wParam; } It is conventional for the event loop to call TranslateMessage on each message which can translate virtual keystrokes into strings.[src] Calling TranslateMessage is not technically required, but problems can result if it is not called.[src] The message loop must call DispatchMessage .[src] The message loop does not directly act on the messages that it handles.[src] It dispatches them by calling DispatchMessage , which transfers the message to the "window procedure" for the window that the message was addressed to.[src] (The "window procedure" is a callback procedure, which got associated with the window class when it was registered.) (More than one window can use the same window procedure.) Code can also send messages directly to a window procedure.[src] These are called nonqueued messages.[src] A strict message loop is not the only option.[src] Code elsewhere in the program can also accept and dispatch messages.[src] PeekMessage is a non-blocking call that returns immediately, with a message if any are waiting, or no message if none is waiting.[src] WaitMessage allows a thread to sleep until a message is in the queue.[src] Modern graphical interface frameworks, such as Windows Forms, Windows Presentation Foundation, MFC, Delphi, Qt, and others do not require applications to code a Windows message loop, because they automatically route events such as key presses and mouse clicks to their appropriate handlers as defined within the framework.[src] However, each framework implements a message loop somewhere, and the message loop can usually be accessed or replaced when more direct control is required.[src] ==See also== Xlib for the event loop in the X Window System ==External links== Meandering Through the Maze of MFC Message and Command Routing (MSJ, July 1995) A Key's Odyssey: the path of a keystroke message through the VCL Platform SDK: Messages and Message Queues Using Messages and Message Queues (MSDN) GetMessage function PeekMessage function[src]
43 sentences · 43 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Message loop in Microsoft Windows." encyclopegia.org, 2025, https://encyclopegia.org/wiki/message-loop-in-microsoft-windows. Accessed 05 December 2026.
Chicago
"Message loop in Microsoft Windows." encyclopegia.org. Accessed December 5, 2026. https://encyclopegia.org/wiki/message-loop-in-microsoft-windows.
APA
encyclopegia.org. (2025). Message loop in Microsoft Windows. Retrieved December 5, 2026, from https://encyclopegia.org/wiki/message-loop-in-microsoft-windows
Accessed December 5, 2026 · CC BY-SA 4.0 · citation page · JSON