Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does async works in C#?

Microsoft announced the Visual Studio Async CTP today (October 28, 2010) that introduces the async and await keywords into C#/VB for asynchronous method execution.

First I thought that the compiler translates the keywords into the creation of a thread but according to the white paper and Anders Hejlsberg's PDC presentation (at 31:00) the asynchronous operation happens completely on the main thread.

How can I have an operation executed in parallel on the same thread? How is it technically possible and to what is the feature actually translated in IL?

like image 322
Dirk Vollmar Avatar asked Oct 28 '10 21:10

Dirk Vollmar


People also ask

How does async work in C?

The idea is simple. The first time you call the async function, it will run like normal until it hits some form of await. Then it may return. Each time after that, the function jumps back to the await statement.

How does the async work?

The async keyword turns a method into an async method, which allows you to use the await keyword in its body. When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete.

How does async work internally?

Under the hood, it's just syntactic sugar using generators and yield statements to “pause” execution. In other words, async functions can “pull out” the value of a Promise even though it's nested inside a callback function, giving us the ability to assign it to a variable!

Does C support asynchronous?

With the new C++11 standard, there is std::async . Pretty much anything the machine is capable of can be done in C, you just have to do it yourself.


1 Answers

It works similarly to the yield return keyword in C# 2.0.

An asynchronous method is not actually an ordinary sequential method. It is compiled into a state machine (an object) with some state (local variables are turned into fields of the object). Each block of code between two uses of await is one "step" of the state machine.

This means that when the method starts, it just runs the first step and then the state machine returns and schedules some work to be done - when the work is done, it will run the next step of the state machine. For example this code:

async Task Demo() {    var v1 = foo();   var v2 = await bar();   more(v1, v2); } 

Would be translated to something like:

class _Demo {   int _v1, _v2;   int _state = 0;    Task<int> _await1;   public void Step() {     switch(this._state) {     case 0:        this._v1 = foo();       this._await1 = bar();       // When the async operation completes, it will call this method       this._state = 1;       op.SetContinuation(Step);     case 1:       this._v2 = this._await1.Result; // Get the result of the operation       more(this._v1, this._v2);   } } 

The important part is that it just uses the SetContinuation method to specify that when the operation completes, it should call the Step method again (and the method knows that it should run the second bit of the original code using the _state field). You can easily imagine that the SetContinuation would be something like btn.Click += Step, which would run completely on a single thread.

The asynchronous programming model in C# is very close to F# asynchronous workflows (in fact, it is essentially the same thing, aside from some technical details), and writing reactive single-threaded GUI applications using async is quite an interesting area - at least I think so - see for example this article (maybe I should write a C# version now :-)).

The translation is similar to iterators (and yield return) and in fact, it was possible to use iterators to implement asynchronous programming in C# earlier. I wrote an article about that a while ago - and I think it can still give you some insight on how the translation works.

like image 146
Tomas Petricek Avatar answered Sep 30 '22 03:09

Tomas Petricek