[ACCEPTED]-What is the use of a Dispatcher Object in WPF?-dispatcher

Accepted answer
Score: 28

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

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

You can find on the following link 1 some more details on the subject: http://blogs.microsoft.co.il/blogs/arik/archive/2010/08/12/wpf-inside-out-dispatcher.aspx

Score: 16

A dispatcher is often used to invoke calls 4 on another thread. An example would be if 3 you have a background thread working, and 2 you need to update the UI thread, you would 1 need a dispatcher to do it.

Score: 0

In my experience we use Prism Event Aggregator. When 2 the event happens it calls the Dispatcher.Invoke() to update 1 the UI. This is because only the Dispatcher can update the objects in your UI from a non-UI thread.

public PaginatedObservableCollection<OrderItems> Orders { get; } = new PaginatedObservableCollection<OrderItems>(20);

_eventAggregator.GetEvent<OrderEvent>().Subscribe(orders =>
      MainDispatcher.Invoke(() => AddOrders(orders));

private void AddOrders(List<OrderItems> orders)
     foreach (OrderItems item in orders)

More Related questions