This recipe describes another very important aspect of dealing with tasks, which is a proper way to work with UI from the asynchronous code. We will learn what a task scheduler is, why it is so important, how it can harm our application, and how to use it to avoid errors.
To step through this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter4\Recipe9
.
For tweaking tasks execution with TaskScheduler
, perform the following steps:
Start Visual Studio 2012. Create a new C# WPF Application project. This time we will need a UI thread with a message loop, which is not available in console applications.
In the
MainWindow.xaml
file, add the following markup inside a grid element (that is, between the<Grid>
and</Grid>
tags):<TextBlock Name="ContentTextBlock" HorizontalAlignment="Left" Margin="44,134,0,0" VerticalAlignment...