Async async await and task for. net4.5

Source: Internet
Author: User

First, you should understand the concept of task-----task//This is a better than thread, or even threadpool things and is the default asynchronous

To understand async and await again:

1, async only with await, used to declare the key word async method;
2. Between async and await, await is the key, the direct meaning-"asynchronous wait", the main thread runs to the await in the Async method, normally the main thread will immediately return to the upper-level method, and the await the next task (method) will continue the new thread execution, the new thread executes " After you wait for the out-of-task (method) result, the remaining portion of the code after the await in the Async method is run.

This is the core of async and await!!!!

The basic structure is as follows

Modifier +async+ return type + method name

{Non-asynchronous content

Await task (Task): You can return a task in another way, or you can directly use delegates, anonymous functions, lambda expressions to write asynchronous code in a task.

What is done after the task is completed this is also done in the newly opened thread.

}

See the code for a specific look:

    Private Static voidMain (string[]args] {            //To invoke an async method:Console.WriteLine ("Main is running on thread"+Thread.CurrentThread.ManagedThreadId);            Fun2async (); Console.WriteLine ("look at that! I went back to the upper level when I was doing an await.");        Console.readkey (); }        //asynchronous methods must be identified with the Async keyword         Public Static Async voidFunasync () {Console.WriteLine ("Funasync is running on thread"+Thread.CurrentThread.ManagedThreadId); //the main thread will return to the upper method to continue execution//await is the core of async this wait function has a very powerful effect: 1. Task new thread 2.await is used to handle this asynchronous            awaitTask.run (() ={Console.WriteLine ("Task is running on thread"+Thread.CurrentThread.ManagedThreadId); Thread.Sleep ( the); Console.WriteLine ("Task is ended");           }); //The code in the task finishes executing the following code will continue to execute in the new threadConsole.WriteLine ("I am in thread"+Thread.CurrentThread.ManagedThreadId); }

Let's look at other forms of expression below:

 //asynchronous methods must be identified with the Async keyword         Public Static Async voidFunasync () {Console.WriteLine ("Funasync is running on thread"+Thread.CurrentThread.ManagedThreadId); //the main thread will return to the upper method to continue execution//await is the core of async this wait function has a very powerful effect: 1. Task new thread 2.await is used to handle this asynchronous            awaitMyTask (); //The code in the task finishes executing the following code will continue to execute in the new threadConsole.WriteLine ("I am in thread"+Thread.CurrentThread.ManagedThreadId); }        Public StaticTask MyTask () {Console.WriteLine ("Now was runing on thread"+thread.currentthread.managedthreadid);//in special cases, this will run in the method that calls the task, and is executed before the task,           returnTask.run (()={Console.WriteLine ("Task is running on thread"+Thread.CurrentThread.ManagedThreadId); Thread.Sleep ( the); Console.WriteLine ("Task is ended");       }); }

Of course, async methods can be nested using!!!!!!!!!!!!!!!!!!!

Just write the subordinate async method to a task, and then await the task with a higher-level async method.

The code is as follows:

//asynchronous methods must be identified with the Async keyword         Public Static Async voidFunasync () {Console.WriteLine ("Funasync is running on thread"+Thread.CurrentThread.ManagedThreadId); //the main thread will return to the upper method to continue execution//await is the core of async this wait function has a very powerful effect: 1. Task new thread 2.await is used to handle this asynchronous            awaitMyTask (); //The code in the task finishes executing the following code will continue to execute in the new threadConsole.WriteLine ("I am in thread"+Thread.CurrentThread.ManagedThreadId); }        Public StaticTask MyTask () {//in special cases, this will run in the method that calls the task, and is executed before the task,Console.WriteLine ("Now was runing on thread"+Thread.CurrentThread.ManagedThreadId); returnTask.run (()={Console.WriteLine ("Task is running on thread"+Thread.CurrentThread.ManagedThreadId); Thread.Sleep ( the); Console.WriteLine ("Task is ended");       }); }       //The following is a nested call to an async method:         Public Static Async voidParenasync () {Console.WriteLine ("Parenasync is running on thread"+Thread.CurrentThread.ManagedThreadId); //The lambda expression for the first column is used here.             awaitTask.run (() ={funasync ();//writes a low-level async method to a task .....                 });        Console.readkey (); }

Everything is done around await and task, and the principle is quite simple.

Here's a summary:

1. Performance: Task>threadpool>thread

The 2.await method followed is either a task or a task<t> type method, or a task object directly;

3. Traditional delegate < anonymous methods The <LAMBDA expression is the same in all three, and is identical to the traditional delegate. Just the latter two more concise, convenient

Task and Lambda need to continue to understand,

Async async await and task for. net4.5

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.