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?

ebb
  • 9,297
  • 18
  • 72
  • 123

1 Answers1

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.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • 1
    So when a button is clicked, the code in event handler is sent as a message to the queue? – ebb Apr 01 '12 at 09:31
  • 1
    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. – Adam Houldsworth Apr 01 '12 at 09:33
  • 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? – ebb Apr 01 '12 at 09:40
  • 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. – Adam Houldsworth Apr 01 '12 at 09:49
  • @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. – Adam Houldsworth Oct 18 '12 at 22:21