A clever method of async & await asynchronous programming, sync
The await keyword does not create a new thread. Instead, it is a thread created by a Task or xxxAsync in FCL. The created thread is a working thread created based on the thread pool and belongs to the background thread.
The await keyword will block/pause calling its method, that is, the following Phycology method. when blocking its call method, the program will return to the UI thread for execution, that is, to execute in the main method. This can be done through
In C #, what are Thread, Task, Async/Await, and IAsyncResult! The program to judge.
You must pay attention to the use of the program snippets of async & await combinations. Here we will list several precautions, which can be said to be optimization considerations. I put it in the comments part of the Code to make it easier for you to understand:
/// <Summary> ///.... recommended books... ///// the returned value of await should be displayed here... /// </summary> /// <param name = "args"> </param> static void Main (string [] args) {// first-class books: psychology .... var book = new List <book> () {new book () {book_classify = "Psychology", book_name = "every kind of loneliness is accompanied"}, new book () {book_classify = "Psychology", book_name = "May you have a life illuminated by love"}, new book () {book_classify = "Psychology ", book_name = "Thank you for your imperfections"}, new book () {book_classify = "Programming Language", book_name = "Write high-quality code to improve the C # program's 157 suggestions "}, new book () {book_classify = "Programming Language", book_name = "python cookbook"}, new book () {book_classify = "Programming Language ", book_name = "design mode (C #)" }}; // var test = book. where (p => p. book_classify = "Psychology "). select (p => p. book_name); // foreach (var I in test) // {// Console. writeLine (I); //} var task = handle (book); Console. writeLine (task. result) ;}/// <summary> /// Attention: // here, we extract the data that we put in the thread pool through the Task for processing and returning, at the same time, note // in addition to passing asynchronous call methods (here is the Phycology method), because other operations may be required, /// we can place the await keyword in the variable returned by the asynchronous call method, instead of directly using the await keyword to wait for the call method. // This design makes full use of the time, improved efficiency... /// </summary> /// <param name = "book"> </param> /// <returns> </returns> public static async Task <string> handle (List <book> book) {var _ physchlogy = Phycology (book); var _ programmer = Programmer (book); // do some other operations here... /// return await _ physchlogy + await _ programmer;} // <summary> // Attention: /// the required data should be returned to the calling method... /// here is sb // </summary> /// <param name = "book"> </param> /// <returns> </returns> public static async Task <string> Programmer (List <book> book) {StringBuilder sb = new StringBuilder (); return await Task. run () => {var programmer = book. where (p => p. book_classify = "Programming Language "). select (p => p. book_name ). toList (); foreach (var I in programmer) {sb. append (I + "\ r \ n");} return sb. toString () ;});} public static async Task <string> Phycology (List <book> book) {StringBuilder sb = new StringBuilder (); return await Task. run () => {var _ phychology = book. where (p => p. book_classify = "Psychology "). select (p => p. book_name ). toList (); foreach (var I in _ phychology) {sb. append (I + "\ r \ n");} return sb. toString ();});}}
The final implementation: