Monday, October 03, 2011

What is a dispatcher Object?

    Every UIElement in WPF is derived from DispatcherObject which defines a property called Dispatcher that points to the UI thread.
    A dispatcher is used to raise calls on another thread.
    Dispatcher can also be called as a  class that handles thread affinity.

Thread Affinity

As mentioned  above Dispatcher thread holds all UI elements. This is called thread affinity. All Most all of the WPF elements have thread affinity. If we have a background thread working, and if at all we need to update the UI thread, then a dispatcher is definitely required for this. Thus from any other thread, if at all we want to access UI component, we need to do it using Dispatcher thread.

DispatcherObject class contains two methods .

1. CheckAccess() : This method provides access to current dispatcher that an object is tied to . This returns a Boolean value as true if the current thread has access to use the object and returns false if the current thread can not use the object.

2. VerifyAccess() : The purpose of this method is to Verify  if the thread has access to the object. If the thread does not have access to the object, an exception is thrown.

If we make a call to a DispatcherObject from a non-UI thread, it will throw an exception. So if we are working on a non-UI thread, we need to update DispatcherObjects by using dispatcher.

The below picture represents object hierarchy in WPF.  It gets inherited from Object class.

WPFobjectheirarchy_5F00_thumb_5F00_112BC62A.png

Read more: Beyond Relational
QR: what-is-a-dispatcher-object.aspx

Posted via email from Jasper-Net