Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does message loop execute on UI thread?

I am so confusing about how message loop in winforms work. I am working on windows form and i knew when calling Application.Run(myform), so it will create a message queue and message loop and then display my Form and start retrieving message in message queue to dispatch.

I read some topics and still do not understand how message loop work behind the scene, UI Thread both running message pump AND executing code? and How does the message loop use threads?

My question is: Does message loop will run on UI thread. If yes why it do not block UI thread?

If question not clear please let me know, sorry because my english is bad.

like image 223
vietvoquoc Avatar asked Jul 15 '16 08:07

vietvoquoc


2 Answers

The message loop can broadly be explained with this example code:

Message message;
while (PeekMessage(out message))
    ProcessMessage(messae);

And yes, this "blocks" the thread. The thread will always either be waiting for a message to appear (PeekMessage should return when a message is ready) or processing the message.

What is important to know is that this is the magic that doesn't block UI. Anything the UI needs to respond to is handled as a message.

The very important part is that to avoid blocking the UI, whatever ProcessMessage does to process a single message should never take too long.

If the user clicks on a button, this will be handled as a message. As part of processing that message, your event handler will be called. If that event handler starts doing something lengthy, you're not returning back to the message loop, and thus it stops processing messages while your button click event handler is running.

So yes, the message loop runs on the UI thread. And yes, it also blocks the UI thread, but the fact that it keeps running means that it isn't blocking the UI.


Inspired by the comment I thought I should post an analogy that explains why the loop works.

Think of a local Deli serving food. Some food is ready-made, just has to be sold to the consumer, and some food has to be prepared on demand.

There are several typical operations happening in this Deli:

  1. An order for food has to be taken
  2. The food has to be prepared, or fetched
  3. The food has to be given to the consumer

Now, consider a Deli with just 1 person behind the desk.

This person does these three operations for each and every consumer. Obviously, if one consumer orders food that has to be prepared then and there, the rest of the consumers will have to wait.

On the other hand, if those orders can be given to a chef, a colleague of the order-taking-employee, then orders will be taken much faster.

In Windows, the message loop is this order-taking-employee, and only him.

As part of processing an order (a message), this guy will have to go check on someone else, and then wait, before taking the food (the results) back to the consumer. Any lengthy processing thus slows down processing of this queue.

On the other hand, if that "someone else" simply says, you go back to the queue and tell the consumer that he will get the food when it is ready, then the queue will be moving, even though someone is now waiting for their food.

This is the typical thread or asynchronous processing that is introduced in order to keep the UI responsive.

like image 161
Lasse V. Karlsen Avatar answered Nov 20 '22 03:11

Lasse V. Karlsen


Does the message loop run on the UI thread?

Yes. It does.

If yes why it do not block UI thread?

You should see the message loop as endless loop: it keeps running forever. Whenever there is a message in the queue to be handled, it picks it up and executes it. That can be a button that is clicked. At that time the button is notified about the event and it will run the code associated to it, for example your button_Click event handler. If you put Thread.Sleep(10000) in it you will notice it will block the entire application for the duration of that sleep (10 seconds). You can't resize, redraw or do anything else because the message loop is blocked too. Once the event handler has ended, it will continue to pick up the next message from the queue.

like image 39
Patrick Hofman Avatar answered Nov 20 '22 03:11

Patrick Hofman