Using the dispatcher's new features

The Dispatcher class is a very common way of accessing the UI thread while we are on another thread.

With WPF 4.5 we have some new methods for synchronous and asynchronous operations, which make this Dispatcher class more async and await friendly. Another improvement is that Dispatcher.Invoke and Dispatcher.InvokeAsync are now able to return a value.

Finally, we also have a new parameter of CancellationToken type, which provides the obvious capability of being able to cancel dispatched tasks.

Getting ready

In order to use this recipe you should have Visual Studio 2012 installed.

How to do it...

In the following steps we will see how to use the Dispatcher class to dispatch a task that accesses the UI thread.

  1. First open ...

Get Microsoft .NET Framework 4.5 Quickstart Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.