Threading 101: What is a Dispatcher?

Posted by Water Cooler v2 on Stack Overflow See other posts from Stack Overflow or by Water Cooler v2
Published on 2010-02-09T20:13:27Z Indexed on 2010/04/18 0:13 UTC
Read the original article Hit count: 619

Once upon a time, I remembered this stuff by heart. Over time, my understanding has diluted and I mean to refresh it.

As I recall, any so called single threaded application has two threads:

a) the primary thread that has a pointer to the main or DllMain entry points; and

b) For applications that have some UI, a UI thread, a.k.a the secondary thread, on which the WndProc runs, i.e. the thread that executes the WndProc that recieves messages that Windows posts to it. In short, the thread that executes the Windows message loop.

For UI apps, the primary thread is in a blocking state waiting for messages from Windows. When it recieves them, it queues them up and dispatches them to the message loop (WndProc) and the UI thread gets kick started.

As per my understanding, the primary thread, which is in a blocking state, is this:

C++

while(getmessage(/* args &msg, etc. */))
{
    translatemessage(&msg, 0, 0);
    dispatchmessage(&msg, 0, 0);
}

C# or VB.NET WinForms apps:

Application.Run( new System.Windows.Forms() );

Is this what they call the Dispatcher?

My questions are:

a) Is my above understanding correct?

b) What in the name of hell is the Dispatcher?

c) Point me to a resource where I can get a better understanding of threads from a Windows/Win32 perspective and then tie it up with high level languages like C#. Petzold is sparing in his discussion on the subject in his epic work.

Although I believe I have it somewhat right, a confirmation will be relieving.

© Stack Overflow or respective owner

Related posts about threads

Related posts about threading