WPF Dispatcher

Kailash Chandra Behera | Wednesday, May 16, 2018

Introduction

In this blog WPF Dispatcher, we are going to discuss what dispatcher in WPF and how to use THE common methods dispatcher in WPF application.

Defination of WPF Dispatcher

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

When you execute a WPF application, it automatically creates a new Dispatcher object and When a Dispatcher is created on a thread, it becomes the only Dispatcher that can be associated with the thread, even if the Dispatcher is shut down. A Dispatcher is also created when you create a DispatcherObject. If you create a Dispatcher on a background thread, be sure to shut down the dispatcher before exiting the thread.

Note:- If a Dispatcher is shut down, it cannot be restarted.

When WPF application starts, it creates two threads:
1. Render thread
2. UI thread

The UI thread is responsible for all the user inputs, handle events, paints screen, and run the application code. Render threads run in the background and used to render the WPF screen.

WPF Dispatcher is associated with the UI thread. The UI thread queues methods call inside the Dispatcher object. Whenever your changes the screen or any event executes, or call a method in the code-behind all this happens in the UI thread and UI thread queue the called method into the Dispatcher queue. Dispatcher executes its message queue into the synchronous order.

Need Of WPF Dispatcher

WPF still follows STA mainly to be interoperable with the earlier Win32/MFC/Winform programming model. A WPF application as such can create and use as many threads as required (to do background processing), but the UI related work will always need to be managed by the main UI thread (also sometimes called the primary thread or dispatcher).

WPF works with Dispatcher object behind the scenes and we don't need to work with Dispatcher when we are working on the UI thread.

When we create a new thread for offloading the work and want to update the UI from the other thread then we must need Dispatcher. Only Dispatcher can update the objects in the UI from the non-UI thread.

WPF Dispatcher Methods

Dispatcher provides two methods for registering method to execute into the message queue.

  1. Invoke

    Executes the specified Action synchronously on the thread the Dispatcher is associated with. Invoke is a synchronous operation, therefore control will not return to the calling object until after the callback returns.

    Example:
      this.Dispatcher.Invoke(() => {  
             txtTextField.Text = string.Empty;  
           });  
    
  2. BeginInvoke
    Begin Invoke method take a Delegate but it executes the method asynchronously. That means it immediately returns before calling the method.
    Example:
     DispatcherOperation op = Dispatcher.BeginInvoke((Action)(() => {  
           btn1.Content = "By BeginInvoke";  
         }));  
    

Related Articles

  1. Resource in WPF
  2. WPF Binding
  3. WPF Style
  4. WPF Converters

Summary

WPF Dispatcher provides services for managing the queue of work items for a thread, here we discussed what is dispacter, type of dispatcher and example of dispatcher. I hope you have enjoyed it a lot.

Thanks