Avoiding Illegal Thread Calls in WPF
Hi Community,
This post is about avoiding the dreaded "Illegal Thread Call" exception in WPF, even when multi-threaded applications are harder to debug and maintain sometimes we need to implement some mechanism based on threads to refresh and update UI elements. WPF can't escape this fact as GDI can't either. Before delving into the details of how accomplish this it's a good time to talk a bit about WPF basics.
The Basics
WPF is a fundamental component in .NET Framework 3.0, along with WCF, WF and CardSpace. WPF is called to be the sucessor of techologies such as GDI and GDI+. GDI has been offering UI services since Windows 1.0 and throughout the years has grown in size and complexity. GDI+ its sucessor was introduced with Windows XP release, the difference between GDI and GDI+ is that the second one is entirely written in C++ and offers more capabilities. Microsoft has been working to offer developers a platform to ease and improve development while at the same time the syntax and way of coding to be the same, that's .NET Framework main purpose. Since .NET Framework 1.0 we've been able to create applications for Windows based on Windows Forms, a rich infraestructure that encapsulates GDI and GDI+, however we're still using UI elements we had 10 years ago, so Microsoft came up with a fresher and modern graphic engine based on DirectX. WPF (codename Avalon) was born. The image depicted below shows WPF main components (The sections in red are the major code portions of WPF). It's almost entirely written in managed code except for Milcore that's native so it enables a tight integration with DirectX.

In User32 and GDI the system works on an inmediate clipping mode (invalid areas) that need to be rendered, in the other hand WPF uses a "painter's algorithm" painting model, this means that instead of clipping each component, each component is rendered from the back to the front of the display. This allows each component to paint over the previous component's display. The advantage of this model is that we can have complex, partially transparent shapes and since it's executing on top of DirectX engine we can embed multimedia elements into UI objects as well. Another advantage found in WPF is the use of a declarative language to create UI elements, XAML. This allows the creation of astonishing UI by graphic designers and the logic behind it by developers.
Avoiding Illegal Thread Calls in WPF
What is an illegal thread call after all? This situation presents whether one tries to update any UI element, such a ListBox by manipulating its items collection from a thread different than the thread that owns the UI element. This is not permitted because the operating system can't guarantee the items collection integrity so it's up to the developer to do that.
In WPF we can find a new property in UI elements called "Dispatcher" that allows through the use of managed callbacks or delegates perform actions on UI elements without incurring in "Illegal Thread Calls".

Kind regards,
Angel