First of all, writing asynchronous code is hard without tooling as you’ll end up creating a maze of twisted little code, which is really hard to understand.
What is Asynchronous Programming ?
- Asynchrony is not about threading, it is about the mode of execution.
- Also, Asynchrony is not about making many threads to do something. Rather it is about making no thread to do something.
- We have moved from the ancient Synchronous model i.e. while I’m waiting there is one thread running (Synchronous) to while I’m waiting there is no thread running (Asynchronous). So, we don’t have to care about the threads at all.
Don’t worry if this is not clear enough right now, I will explain all these as we go along this post.
Another important aspect of asynchronous programming that we must understand is that Asynchrony ! = Parallelism.
Asynchrony can be taken as the way of removing threads. When you have concurrency it means that you want to do many things at the same time in your code; this does not necessarily mean that you need parallelism or multiple threads of execution. If you can give the work load to someone else and react when they are finished, you can achieve a great deal of concurrency with very little parallelism.
Where we can use this?
In an execution model where we are asking for something which is not going to or might not come back straight away or which is not even guaranteed to come back.
We are used to a programming model where we ask a question and we do nothing until we get the answer. But, it is not like that in the real world at all. We don’t make an online order and wait at our doors until it arrives, rather we engage ourselves with other work.
Now, let’s look under the hood of c# async and await and see how to use it.
I have seen at a lot many places in our code where we do…
Await something async
Await something async
Await something async..
Await is not just a method that returns a task of T. Basically, it is to start something and give me back a token. And, the token we use in .net is a task of T.
How to do asynchronous programming with Async and Await in C#?
Async keyword is applied to a method which informs the compiler that we want to use asynchronous features. This keyword does not change the method signature neither does it change how the method is used, it only changes how the method is compiled.
Applying an async keyword to a method doesn’t make it asynchronous. Async simply declares that we want to use await keyword but we are not bound to do so. Using await keyword makes a big difference in our code and how it looks and works after compilation.
Now, lets take an example code:
In this code, the worker class needs to do some expensive work which needs to get done while the user waits. When we run this, we will not see any progress indicators as our LongOperation() is synchronous, which means it will block until its done.
Now, let’s add Aysnc keyword to our DoWork method and see what happens.
It will actually do the same thing it did without the async keyword but this async keyword had changed our code during compilation. Let’s look at the complied code. I have used ilspy to decompile the assembly.
The async methods are converted into asynchronous code by building and using a state machine.
If we go through the code, we can actually see the code that starts our state machine to do work by invoking the MoveNext method on our state machine.
If we look closer, we will see that the context switching is being taken care of by this method. And, we really don’t have to worry about threads when we are using async in c#.
Now, let’s apply await to the LongOperation() method and as we cannot await a void method, we need our long operation method to return a task.
Let’s take a look at the entire code and see what has changed in our program.
So, now we have seen what happens when we write async code. We must understand that Asynchronous methods using async and await in c# does not come for free, there is a cost associated with it, which should be considered before using it anywhere.
Just because you can, does not mean you should!
Update – 19 Aug ’15
As mentioned in this really good post by Stephen Cleary, Task.Factory.StartNew can be dangerous because of its default values and it is safer to use Task.Run.