Dispatcher - How does it work?

After having read the documentation for the Dispatcher class, I realize that it can be used for non-UI queueing of actions too.

So how does the Dispatcher class actually works? I'm aware of that, it's main job is to queue actions to a specific thread - but how does it "send" those actions to the thread? And how do the thread "get" these actions?

My best guess is that there's some kind of "thread queue" for each thread, but then again I've got no idea.

Answers


It is not a trivial task to get one thread to initiate execution of code onto another thread. The crux of the problem is that you cannot simply tell any thread to start executing a method after that thread has already started. The target thread has to be specifically setup to receive these kind of requests ahead of time.

The usual pattern used is the producer-consumer. The target thread will spin around an infinite loop waiting for messages to appear in a blocking queue. The queue is designed to block until an item appears in the queue thus preventing the target thread from consuming CPU time unnecessarily. Here is a really simple way to get a thread to accept the injection of a delegate for execution.

public class Example
{
  private BlockingCollection<Action> queue = new BlockingCollection<Action>();

  public Example()
  {
    new Thread(
      () =>
      {
        while (true)
        {
          Action action = queue.Take();
          action();
        }
      }).Start();
  }

  public void ExecuteAsync(Action action)
  {
    queue.Add(action);
  }
}

Now, in the case of a UI thread it already has a message loop running so the Dispatcher class can simply post a special message to the message queue containing the delegate to be executed. In the middle of processing all of the paint, button clicks, etc. this special message will be picked up by the UI thread as well and it will begin executing the delegate.

So how does the Dispatcher class actually works? I'm aware of that, it's main job is to queue actions to a specific thread - but how does it "send" those actions to the thread?

By queuing a delegate into a queue that the target thread monitors.

And how do the thread "get" these actions?

By running an infinite loop that monitors the queue. The queue is usually a special type called a blocking queue which blocks the consuming thread if the queue is empty.

My best guess is that there's some kind of "thread queue" for each thread, but then again I've got no idea.

Pretty close. Except that threads do not actually have a built in queue for this purpose. It has to be manually setup. That is why only threads that are specifically designed can accept delegate injections. UI threads are setup this way because Application.Run creates the message loop. In my example you will see that I had to use BlockingCollection and an infinite loop to get it to work on a worker thread.


Well, apparently the Dispatcher queuing implementation looks like a Win32 message pump but it's not (although it uses the same User32 messages and threading model).


Very interesting question, But you don't need a Window to have a message queue, these are separate concepts, you can create a message queue on any thread just by calling PeekMessage.

For more information take a look at here

but of course it doesn't mean that a Dispatcher without a window is of any use. I can assume that the designers were thinking about an independent Dispatcher object in order to let it handle as many window as the application have.


Need Your Help

How do I convert this HTML-table layout solution to floating-div solution?

html css html-table css-float

I often need to list items with various-sized images on the left and text on the right, like this:

MSBuild task does not cause generated c# files to compile

c# visual-studio-2010 msbuild

I've been trying to write an MSBuild task to "compile" CoCo/R .ATG files into C# files which will then get compiled into the executable, this is to replace the pre-build event.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.