What’s the difference between Task.ContinueWith and await?

In the .NET there are many ways to implement asynchronous programs.

We can use the Task, Task<T> or await keyword to write asynchronous programs.

For Example:

Task<IPAddress[]> addresses = Dns.GetHostAddressAsync(“msdn.com”); addresses.ContinueWith(parameter =>      {

IPAddress[] ipAddresses = addresses.Result;


As we’ve seen, we the Task<T> start the asynchronous operation and the ContinueWith method register for the result callback.

However in .NET 4.5 and c#5, we can use the await keyword to write the same asynchronous programs.

For Example:

private async void ProcessDnsAdressAsync() {

IPAddress[] addresses = await Dns.GetHostAddressAsync(“msdn.com”);


So what’s the difference between them?

  1. In .NET 4 the asynchronous method we intending to write have to split into two methods: the actual method and the callback. This creates code that is hard to maintain, debug and follow.
  2. What happens if we want to call many asynchronous calls in a loop? The only option is to use recursive method, which is much harder to maintain and follow. That what the .NET4.5 solved.
  3. In the case of UI application, that means we want to update the UI with the data that returned from asynchronous call. If we use the ContinueWith, we can’t update UI data within the ContinueWith callback, because the execution of the callback will not run on the UI thread. But on the other hand we can safety manipulate UI data after the await keyword, because the UI thread will continue executing after the await statement.


Behind the scenes the compiler generate the same logic for both scenarios, but the async and await, give us very nice and maintainable code for asynchronous programming.


About Idan Reuven

Idan is Microsoft Certified Proffesional Developer (MCPD) Idan Working As Senior Software developer. and specialize in professional application development with WPF, WCF, Entity Framework, XAML, HTML 5, Java Script , jQuery, MVC, MVVM, C#, C++, SQL Server Technologies.
This entry was posted in .NET, .NET 4.5, C# 5. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s