Use async await on c# 🚀 (easy way)
Tags: csharp, tutorial, programming, dotnet
Published: 2022-01-08 03:38:47

Do you want to use async/await on c# ? (yes, as in javascript) Okey, I'll show you how to do that in an easy way 😎

#### First of all
You should have your project on c# (.net core/framework) created.

## Let's start 
There are a few concepts that you need to know, so this tutorial is divided into several small sections:

{% details __Task class__ 🧙 your best friend %}
The task class on c# represents an async operation, this operation can be a simple method wich will be called without interrupt the current process thread, it'll run on another Thread, but you don't have to do any complex syntax, just call a method. 
 

**This is the basic usage:**
![Basic usage of Task class](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t0l0znrndqu4ox7qfst6.png)

We can see, 3 elements:

- The Task class constructor and variable task1
- A lambda expression (is like an arrow function): that represent the operation wich we want to run ([lambda expression in c#](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions)).
- Start method call: run the operation asynchronously

**Output:**
```
Hello! this will run async
```
 
{% enddetails %}{% details **A more detailed example** 👀 %}
The task class is a powerful tool of c#, but maybe you got confused on **how and when it is running**. 

Well this is a more detailed example, we have 2 tasks to do:
![Task examples](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41csm59aye7xx5bjthwg.png)

As you can see, the 2 tasks are _example operations_ wich use `Thread.Sleep(miliseconds)` function to stop temporaly the task thread (to simulate a _real_ operation).

So, let's `Start` these tasks and check it's behaviour:
![Detailed task example code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6avbjp8a48ne33baa73l.png)
 

**Output**
```
Starting all tasks
Starting task 1
Starting task 2
------------------------------------
All tasks have finished, or not?
Task 1 done
Task 2 done
```

As we can see, the 2 tasks have been called asynchronously, and it's output is on the bottom.

But what happend with the `All tasks have finished, or not?` line, it should appear after tasks have been done, how can we do that? It's very easy, and we'll see it on the next section
{% enddetails %}

{% details **Async/Await** 🪄  %}
Async/Await in c# is simple. You just have to put the `async` word in a method, example:
`public void DoSomething()` to `public async void DoSomething()`.

Now, this method is able to "await" a `Task` until it finishes its work, using the `await` word before the task name 🤯

---

So, we can modify the example of the previous section to make it an async method:

![Async Await example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ovvtt1pvhrs3ud9f2s6e.png)

As we can see, the code is inside of an async method called `Main`. Also, we add `await task`, this will allow us to control the code flow.

Now, the task 1 will start with `task1.Start()` normally, but the `await task1` statement won't allow the code execution to continue until the task 1 has finished. The same behaviour for the task 2.

The output:

```
Starting all tasks

Hello! this will run async
Starting task 1
Task 1 done
Starting task 2
Task 2 done
------------------------------------
All tasks have finished, or not?
```

We have fixed the problems we saw in the previous section.
- The line `All tasks have finished, or not?` is at the end
- Also, both tasks run as a synchronous operation, but with the advantages of Async/Await features 💪

{% enddetails %}

![Async meme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6rmoin9hvlf1djjpib7.png)
 
---

The Async/Await and **Task** class are powerful tools to make concurrent, efficient and legible code without much effort 🚀.

There are more useful uses of these features, you can leave me a comment if you want more tutorials about C# or something else 👀