Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / WPF

WPF Inside Out: Dispatcher

Rate me:
Please Sign up or sign in to vote.
4.81/5 (11 votes)
15 Sep 2010Ms-PL2 min read 47.2K   14   4
In this post, we’ll see a few interesting things about WPF’s Dispatcher class.

Back in this post, I showed you how you can have a look at the original source code of .NET, including original comments and variable names.

In this post, we'll see a few interesting things about WPF’s Dispatcher class. But first, some background on the subject.

WPF Thread Affinity

Almost every WPF element has thread affinity. This means that access to such an element should be made only from the thread that created the element. In order to do so, every element that requires thread affinity is derived, eventually, from DispatcherObject class. This class provides a property named Dispatcher that returns the Dispatcher object associated with the WPF element.

The Dispatcher Class

The Dispatcher class is used to perform work on his attached thread. It has a queue of work items and it is in charge of executing the work items on the dispatcher thread.

So, when you want to change a property of a WPF element from a thread different from the one who created the element, you should use the element’s Dispatcher property to dispatch the operation to the correct thread. This is done using the BeginInvoke method that accepts a method to be invoked.

Where is the Dispatcher of the Thread Saved?

Every thread should have their dispatcher object, so you would think they will save the dispatcher on the Thread Local Storage (TLS).

It turns out they store a static list of all available dispatcher objects.
Of course, this list is synchronized using a private</span /> global static</span /> object (this is a common best practice when locking object).

Whenever the dispatcher of an object is needed, they go over the list, comparing the dispatcher’s Thread property with the current thread, until they find the correct dispatcher object for this thread.

The reason, as they note in the comments is that managed TLS is rather expensive.

Above this list, they add the following optimization: before going over the dispatchers list, they check if the last given dispatcher is suitable, so only a thread context switch will derive a new list search.

Although strange, this usually isn't such a problem because most applications will only have one thread that creates UI elements, hence only one Dispatcher in the list.

That’s it for now,
Arik Poznanski

This article was originally posted at http://feeds.feedburner.com/ArikPoznanskisBlog

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.

Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).

Comments and Discussions

 
GeneralMy vote of 5 Pin
Gary Wheeler21-Sep-10 0:11
Gary Wheeler21-Sep-10 0:11 
GeneralIt isn't intermediate, but... Pin
Yury Goltsman20-Sep-10 22:25
Yury Goltsman20-Sep-10 22:25 
GeneralRe: It isn't intermediate, but... Pin
Gary Wheeler21-Sep-10 0:15
Gary Wheeler21-Sep-10 0:15 
GeneralRe: It isn't intermediate, but... Pin
Yury Goltsman21-Sep-10 4:33
Yury Goltsman21-Sep-10 4:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.