Your Ultimate async / await Tutorial in C#


Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

You might have already heard about asynchronous programming, either in .Net or some other language. In this article I will try to explain it to you, how to understand the concept easily, why and when we write asynchronous, the structure of async / await and I will include some examples to better explain the concept.

With asynchronous programming, you can divide your logic into awaitable tasks, where you can perform some long running operations such as reading large file, doing an API call, downloading a resource from web or performing a complex calculation without having to block the execution of your application on UI or service.

.Net framework gives you simple and easy to use keywords which are the async and await modifiers to transform your code from synchronous to asynchronous.

So you can call an async method separately by obtaining a task object to it, then do some unrelated work, and after that await that task, either it has already finished which will result in returning the value from the task and then using it in next statements, or if it has not finished yet, then the execution will go back to the caller of the async method, and the program will execute normally without blocking the UI or the running service, and then once the task is done, it will proceed with the rest of the async method and return.

There are 3 patterns for asynchronous programming: Asynchronous Programming Model (APM) pattern, Event-based Asynchronous Pattern (EAP) and the most recent one, Task-based Asynchronous Pattern (TAP).

Currently, it is recommended to only follow the Task-based Asynchronous Pattern whenever you want to have an asynchronous implementation in your application, and on top of it, you can use async / await feature to further enhance the structure of your application, making your code more organised and better maintained.

With a few slight changes on your code, you will be able to gain the benefits of asynchronous programming into your project.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content