ArticleslgStudy

computer science

Message loop in Microsoft Windows

Message loop in Microsoft Windows is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Message loop in Microsoft Windows rather than just read about it. In short: The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows. Windows programs that have a GUI are event-driven.

Key takeaways

  • Message loop in Microsoft Windows belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Message loop in Microsoft Windows to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Message loop in Microsoft Windows from memory before moving on to harder problems.

Reference excerpt

The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows. Windows programs that have a GUI are event-driven. Windows maintains an individual message queue for each thread that has created a window. Usually only the first thread creates windows. 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. A process can also add messages to its own queue. To accept user input, and for other reasons, each thread with a window must continuously retrieve messages from its queue, and act on them. 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. This is the message loop. 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. Messages for every window of the process pass through its message queue, and are handled by its message loop. A message loop is one kind of event loop. A basic message loop appears as follows:

It is conventional for the event loop to call TranslateMessage on each message which can translate virtual keystrokes into strings. Calling TranslateMessage is not technically required, but problems can result if it is not called. The message loop must call DispatchMessage. The message loop does not directly act on the messages that it handles. It dispatches them by calling DispatchMessage, which transfers the message to the "window procedure" for the window that the message was addressed to. (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. These are called nonqueued messages. A strict message loop is not the only option. Code elsewhere in the program can also accept and dispatch messages. PeekMessage is a non-blocking call that returns immediately, with a message if any are waiting, or no message if none is waiting. WaitMessage allows a thread to sleep until a message is in the queue. 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. However, each framework implements a message loop somewhere, and the message loop can usually be accessed or replaced when more direct control is required.

See also Xlib for the event loop in the X Window System

References

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

Worked examples

Example 1 — a first encounter with Message loop in Microsoft Windows

Start with the simplest possible case. Write down what Message loop in Microsoft Windows claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Message loop in Microsoft Windows before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Message loop in Microsoft Windows ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Message loop in Microsoft Windows

In research
Message loop in Microsoft Windows appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Message loop in Microsoft Windows in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Message loop in Microsoft Windows is common in secondary-school and first-year university syllabi. It links to neighbouring topics Events (computing), Microsoft application programming interfaces, so understanding it makes those chapters shorter.
In everyday life
Look for Message loop in Microsoft Windows outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Message loop in Microsoft Windows in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Message loop in Microsoft Windows means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Message loop in Microsoft Windows out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Message loop in Microsoft Windows in simple terms?

The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows. Windows programs that have a GUI are event-driven.

Why does Message loop in Microsoft Windows matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Message loop in Microsoft Windows?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Message loop in Microsoft Windows.

Tags

  • Events (computing)
  • Microsoft application programming interfaces

Keep exploring