[Async] [await]

Source: Internet
Author: User

In the previous article on [foreach], I found that [yield] has not understood [yield] clearly, I think that I have read a story from Daniel (here). I will review it and write my own understanding here to share with you.

I. Normal Asynchronization

Now let's assume that there are three methods that we often encounter. funcOne and funcTwo need to be executed asynchronously in a time-consuming manner, in addition, their logic is that funcTwo can be executed only after funcOne is executed. Similarly, funcThree can be executed only after funcTwo is executed.

According to this setting, see the code segment [1].

1 public class Program 2 {3 public delegate void CallBack (string nextName); 4 public void funcOne (CallBack callback) 5 {6 Console. WriteLine ("[One] async Continue! "); 7 Console. WriteLine (" [One] do something! "); 8 callback (" Called Two "); 9} 10 public void funcTwo (CallBack callback) 11 {12 Console. WriteLine (" [Two] async Continue! "); 13 Console. WriteLine (" [Two] do something! "); 14 callback (" Called Three "); 15} 16 public void funcThree (CallBack callback) 17 {18 Console. WriteLine (" [Three] do something! "); 19 callback (" Called... "); 20} 21 static void Main () 22 {23 Program p = new Program (); 24 // asynchronously execute funcOne25 ThreadPool. queueUserWorkItem (state1) => {26 p. funcOne (name1) => 27 {28 Console. writeLine (name1); 29 // asynchronously execute funcTwo30 ThreadPool. queueUserWorkItem (state2) => 31 {32 p. funcTwo (name2) => 33 {34 Console. writeLine (name2); 35 // execute funcThree36 p. funcThree (name3) => 37 {38 Console. writeLine (name3); 39 // Of course there is 40 Console. WriteLine ("End! "); 41}); 42}); 43}); 44}); 45}); 46 Console. Read (); 47} 48}Asynchronous implementation

I believe that after reading the code, we feel the same, so tedious, that is, continuous nesting! Is there a way to avoid this? That is to say, write asynchronous programs in synchronous mode.

Ii. Improved Asynchronization

This [yield] is now available. first look at the code segment [2]:

1 // the definition of the three methods and the delegate CallBack remains unchanged, which is not listed here. 2 // added the static global variable enumerator and the static method funcList. 3 public static System. collections. IEnumerator enumerator = funcList (); 4 public static System. collections. IEnumerator funcList () 5 {6 Program p = new Program (); 7 // asynchronously execute funcOne 8 ThreadPool. queueUserWorkItem (state1) => 9 {10 p. funcOne (name1) => 11 {12 enumerator. moveNext (); 13}); 14}); 15 yield return 1; 16 Console. writeLine ("Called Two"); 17 // asynchronously execute funcTwo18 ThreadPool. queueUserWorkItem (state2) => 19 {20 p. funcTwo (name2) => 21 {22 enumerator. moveNext (); 23}); 24}); 25 yield return 2; 26 Console. writeLine ("Called Three"); 27 // execute funcThree28 p. funcThree (name3) => 29 {30 // Of course, nesting may continue 31}); 32 Console. writeLine ("Called... "); 33 Console. writeLine ("End! "); 34 yield return 3; 35} 36 37 // The changed Main function 38 static void Main () 39 {40 enumerator. moveNext (); 41 Console. read (); 42}Improved asynchronous writing

Now let's see if it's refreshing and there is no nested limit. In the code, we only need to define an iterator. In the iterator, call the method to be executed synchronously and use [yield] to separate the methods, each method is synchronized by calling the MoveNext () method of the iterator in callback.

Through this practice, we can understand that every time [yield return...], the program will temporarily Save the [Context Environment] of the iterator, wait until MoveNext (), and then call it out to continue to the next [yield return...].

3. I think too much.

The above is purely nonsense. After. net 4.5, we have the artifact async/await. Let's take a look at how concise it is.

Code segment [3]:

 

1 public class Program 2 {3 public async Task funcOne () 4 {5 Console. WriteLine ("[One] async Continue! "); 6 Console. WriteLine (" [One] do something! "); 7 // await... 8} 9 public async Task funcTwo () 10 {11 Console. WriteLine (" [Two] async Continue! "); 12 Console. WriteLine (" [Two] do something! "); 13 // await... 14} 15 public void funcThree () 16 {17 Console. WriteLine (" [Three] do something! "); 18} 19 public static async Task funcList () 20 {21 Program p = new Program (); 22 await p. funcOne (); 23 await p. funcTwo (); 24 p. funcThree (); 25 // endless nesting can continue await. 26 Console. WriteLine ("End! "); 27} 28 static void Main () 29 {30 funcList (); 31 Console. Read (); 32} 33}Async/await

 

I have been amazed by you.

The async modifier specifies the method as an Asynchronous Method (note! : Asynchronous is not asynchronous, and async does not have the final say. If there is no await statement in this method, even if the async modifier is used, it will still be executed synchronously because it does not have an asynchronous gene ).

After an Asynchronous Method is specified, the return value of the method can only be Task, Task <TResult>, or void. The returned Task object is used to represent this Asynchronous Method, you can control the Task object to operate on this Asynchronous Method. For details, refer to the Task explanation and sample code in msdn.

Await is used to suspend the main thread (the main thread here is relative), wait for the Asynchronous Method to be executed and return, and then continue to execute the main thread method. With await, the main thread can obtain the Task object returned by the asynchronous method, so that it can be operated in the main thread. If an asynchronous method is called without await, It will be executed asynchronously, and the Main thread will not wait, just like calling the funcList method in the Main method in [3] of our code segment.

 

Finally, let's analyze it here. I hope you can discuss it and make progress together.

Of course, don't be stingy with your [like] Oh :)

 

 

  

 

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.