This Post is the first of 3 Postsabout Asynchronous Programming in .Net.

 

What is Asynchronous Programming?

Asynchronous programming is really all about doing simultaneous processing, meanwhile the user is doing something in your application you can go ahead and do other things that don’t interrupt the user’s workflow in your application. It also means that when we are doing an asynchronous operation, you can get notified about the result when that is available

I’m going to start by taking a look at the Task Parallel Library, which will allow us to start off asynchronous operations and help us see how we can use the Task Parallel Library to set up continuations and ultimately to understand these asynchronous operations a lot better.

Here I have a WPF application that simulate getting stock quotes from the web.

When I click on get quotes button the application stops responding for some seconds, if we take a look inside you can see I’m using Thread.Sleep to simulate a slow database call or a slow call to a web service.

private void GetQuotesBtn_Click (object sender, RoutedEventArgs e) {
  Thread.Sleep (3000);
}

Now I need to introduce something to make this application feel a lot quicker. I’m going to introduce something called the Task Parallel Library. Essentially what I’m going to use in here is something called a task that is how asynchronous operations are represented. The Task class is part of the .NET Framework and lives in System.Threading.Tasks.

Let’s say that I want to change the get quotes button and status label. I want to disable the get quotes button when I click it and then when I’m done I want to re-enable the button and change the text of the status label. Let’s say that after this task completed, I want to do something. Now the continuation also takes an action and this action expects a task to be passed to it. That means that in a continuation I’m going to get the task that actually complete it.

private void GetQuotesBtn_Click (object sender, RoutedEventArgs e) {
  getQuotesBtn.IsEnabled = false;
  var task = Task.Run (() => {
    Thread.Sleep (3000);
  });

  task.ContinueWith ((t) => {
    getQuotesBtn.IsEnabled = true;
    statusMessageLabel.Content = "Quotes Received Successfully";
  });
}

The get quotes button is now disabled but after three seconds nothing else happens. I was able to disable the button but clearly I wasn’t able to enable it again or setup the status label. Also you can notice I’m getting an exception, and this exception is telling me that I’m trying to invoke the UI from a different thread.

Essentially what I want to do is I want to invoke the UI from inside my continuation.

In WPF is used something called a dispatcher. The dispatcher will allow us to invoke something on the UI thread by calling the method invoke. And as you see here it’s telling us that it’s going to execute a specific action synchronously on the thread that System.Windows.Threading.Dispatcher is associated with which is essentially in this case the UI thread.

private void GetQuotesBtn_Click (object sender, RoutedEventArgs e) {
  getQuotesBtn.IsEnabled = false;
  var task = Task.Run (() => {
    Thread.Sleep (3000);
  });

  task.ContinueWith ((t) => {
    Dispatcher.Invoke (() => {
      getQuotesBtn.IsEnabled = true;
      statusMessageLabel.Content = "Quotes Received Successfully";
    });
  });
}

I’m going to set up an anonymous method that simply sets  status label to “Quotes Received Successfully” . With this in place the get quotes button will be disabled and after three seconds, get quotes button will again change its state to enabled.

Next I’m going to change this to return a value from Task.Run and use that to set the value of the button.

private void GetQuotesBtn_Click (object sender, RoutedEventArgs e) {
  getQuotesBtn.IsEnabled = false;
  var task = Task.Run (() => {
    Thread.Sleep (3000);

    return "Quotes Received Successfully";
  });

  task.ContinueWith ((t) => {
    Dispatcher.Invoke (() => {
      getQuotesBtn.IsEnabled = true;
      statusMessageLabel.Content = t.Result;
    });
  });
}

This was basically an introduction to my next Post Async and Await in ASP.NET.
In the next Post I’ll talk about Async and Await in ASP.NET.