Sony Arouje

a programmer's log

Using await in real world asynchronous programming–Part 2

with one comment

This is the continuation to my previous post related to new async ctp. In the previous post I explained the use of await using a webclient. This post I am going to explain how can we use await in real life scenario. In real life you may not only deal with webclient, you may wanted to do a time consuming database call or you wanted to do some time consuming process or whatever in asynchronous manner, at the same time our UI should be responsive.

I did a lot of search to find a scenario to deal expensive process other than calling a webclient but no use. So I thought it will be good to post some real life scenario as most of the blogs explain the async ctp with webclient or in some complex way that person like me cant understand.

Async CTP simplify the way we write asynchronous methods, with async ctp no BeginXXX and no EndXXX. So let’s go through the scenario with a small intro.

Currently am in the process of rewriting one of my app called iTraveller. My prime focus is a very responsive UI with less app loading time. The app requires to load categories and lot of thumbnails at startup. I am doing lot of these process at startup and if we do it synchronously it will affect the application startup speed. When async CTP launched, I jumped into to it, because I know that it’s very useful for me.

Below is a piece of code we normally write to load some data from a database.

public List<LocalCategory> GetAllCategories()
    IRepository repository = GenericRepository.GetRepositoryInstance();
    return repository.GetAll<LocalCategory>().ToList<LocalCategory>();


Here I used the Generic repository model I explained earlier in my blog. The above method is a synchronous call and the caller should wait till the db calls complete. Am going to rewrite the above method in async mode.

public async Task<List<LocalCategory>> GetAllCategoriesAsync()
    IRepository repository = GenericRepository.GetRepositoryInstance();
    return await TaskEx.Run(() => 
        //write the time consuming process here
        return repository.GetAll<LocalCategory>().ToList<LocalCategory>();
    }, System.Threading.CancellationToken.None);


Here I converted a synchronous method to an asynchronous one, It’s as simple as that. Let’s see how can we call the above method.

private async void LoadExistingCategory()
    CategoryService categoryService = new CategoryService();
    var categoriesTask=await categoryService.GetAllCategoriesAsync();
    this.ExistingCategories = categoriesTask.ToList<LocalCategory>();


You can call the async method in button event handler or where ever you want to call. But the caller should marked as async, the above method I added async just after the private.

LocalCategory is one of my entity class in the application.

I wrote a very expensive Euclidean distance algorithm using async mode and it worked very well. The same without async will keep the UI busy for two seconds and the user will be blocked from doing any action. 


Written by Sony Arouje

December 9, 2010 at 11:20 pm

One Response

Subscribe to comments with RSS.

  1. […] my other post to see how to use await in real life scenario like doing a time consuming database call or doing a […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: