2

The UI thread in Winforms have the responsibility of running the message pump, by calling Application.Run. By message pump I mean an endless loop that keeps pulling messages out from the queue.

So now is my question, how can the UI thread also execute a block of code when ex. a click handler is triggered? It should be busy with the message pump, and not able to execute the code?

1 Answer 1

2

It takes time out of processing the message pump to handle the message, as this is synchronously called from the message pump (via the click event).

This is why expensive code can cause the UI to hang.

Sign up to request clarification or add additional context in comments.

5 Comments

So when a button is clicked, the code in event handler is sent as a message to the queue?
The windows message for the click is processed by the MP, internals to the WinForms code calls the click event, and any subscribers of this event will be run synchronously - a click handler is one such subscriber.
Ah makes sense! - But surely, the message that's processed by the MP, have to contain some kind of information about what click event that should be called, since there may also be a click event for another button?
The basic windows messages tend to be just coordinates that are then translated in to the control that was actioned by "hit testing". This starts to get into the underlying Win32 stuff that I don't know much about. For WinForms all you need to know is life is synchronous on the one thread.
@brumScouse Yeah... but I was referring to the UI thread usually being just the one. Nothing stopping you from having multiple different UI message pumps though I suppose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.