It is high time we threw old Background Worker and got used to the new .NET 4.5 Async

Emran Hussain,
Apr 14, 2013

.NET 4.5 has arrived.

The most significant feature of this .NET Framework version is its Asynchronous programming feature in C# and VB.NET. We do not need to write Spaghetti code anymore for asynchronous programming in C#. Even though it may not be comfortable and practical to upgrade all legacy code to .net 4.5 async await, still I find it highly tempting to replace old Background worker based Spaghetti code to new .NET 4.5 async await. When we think about learning asynchronous programming, there are several things we look for,..... How to cancel the task, how to update a UI as progress change... Etc. So, I have organized these usages in a complete little application that I have attached here. This article will not discuss all the details about the async-await feature of .NET 4.5, rather, this article will present you a simple working model so that you can quickly upgrade your old Background worker based application to the async await based application., it is for programmers who already understand code syntax, and don't have time to learn all the details about async await feature of .net 4.5 before upgrading their code from Background worker to async await. It is really worth upgrading, as you can apply more patterns to your code, organize cohesive responsibilities into a single method, instead of scattering here and there. In this article, I have chosen Windows Form platform to demonstrate this feature.

The attached sample application (c# async await example) is programmed to do a very simple task. Here is the Use Case.

use_caseRandom_ Number_ Generator_ GUI

It will Generate a new Random number at random interval. I have used Thread. Sleep (..) for simulating long, time-consuming task. Here is the screenshot of the User Interface of my sample application when running.


The Run button will call the async operation, which is generating a random number and append that number to the List Box shown in the User Interface. The Cancel Button will cancel the operation, and there is a progress bar which will show the total number of calls made by the program after hitting the Start button.

Here is the snapshot of the C# async await example source code (code behind file of this Form). The full source code is attached.


Even though it is not recommended to return 'void' with async keyword, rather you should return 'Task' object. But, for the simplicity of this tutorial, I have not discussed in details about that. The main purpose of this article is to convert the existing Event-based Asynchronous code to the new async/await based code quickly.

Run_ Click_event_handler

code_part 3

To visualize the Run button operation here is a Sequence Diagram.


I hope, this model can help you to upgrade the asynchronous code in your legacy application.

Last Modified at Sep 12, 2018
© All content, photographs, graphs and images in this article are copyright protected by SpiceLogic Inc. and may not, without prior written authorization, in whole or in part, be copied, altered, reproduced or published without exclusive permission of its owner. Unauthorized use is prohibited.

Please feel free to share your comment.