How to Understand The async and await design patterns and how to apply the frameworks below. net 4

Source: Internet
Author: User

I have developed some silverlight things, which are terrible for asynchronous programming. It is very uncomfortable to tear the logic apart.

Today, I saw a good explanation on the stackOverflow website, excerpted it and gave it a try,

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 );
}

Wocould be translated to something like:

Class _ Demo {
Int _ v1, _ v2;
Int _ state = 0;
Task <int> _ await1;
Public void Step (){
Switch (this. _ state ){
Case 0:
Foo ();
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 shoshould call the Step method again (and the method knows that it shoshould run the second bit of the original code using the _ state field ). you can easily imagine that the SetContinuation wocould be something like btn. click + = Step, which wocould run completely on a single thread.

The asynchronous programming model in C # is very close to F # asynchronous workflows (in fact, it is essential to 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 shoshould 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.

Answered by Tomas Petricek

Generally, async and await simulate a state machine (the above _ state variable maintains the state). async splits the execution of a sequence into three parts, and the other is the preface of time-consuming work, one is time-consuming work, and the other is time-consuming follow-up work,

Async Task Demo (){
Var v1 = previous work ();
Var v2 = await time-consuming ();
Subsequent work (v1, v2 );
}

Translated as a State Machine

Class state machine {
Int _ v1, _ v2;
Int state variable = start state;
Task <int> _ await1;
Public void Step (){
Switch (this. status variable ){
Case start state:
Foo ();
This. _ v1 = work in the forward Order ();
This. _ await1 = time-consuming work ();
// When time-consuming work is completed asynchronously, the asynchronous work changes the state variable value.
This. status variable = successful completion;
Continue to call itself, that is, the Step function; // op. SetContinuation (Step );
Case:
This. _ v2 = this. _ await1.Result; // time-consuming work result
Subsequent work (this. _ v1, this. _ v2 );
}
}

In fact, we can also. net framework 4 and below want to call asynchronous functions like the above state machine class, so that the logic of synchronous calling is still maintained, it is better understood, and it is much easier to process the loop.



From the column cjq1234

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.