What is asynchronous programming

What is asynchronous programming it? Here is a simple example:

using System.Net.Http;
using System.Threading.Tasks;
using static System.Console;

namespace Core
{
    class Async
    {
        static void Main()
        {
            Start();
            End();
        }

        static void Wait()=>WriteLine("waiting...");
        static void End()=>WriteLine("end...");
        static int Start()
        {
            WriteLine("start...");
            HttpClient client = new HttpClient();
            Waiting();
            var result = client.GetStringAsync("https://www.visualstudio.com/");
            string str = result.Result;
            return str.Length;
        }
    }
}

In the above code, Main code in the method are performed in order from top to bottom. When poor network conditions, Start()the method is time-consuming (note, here Startcalled asynchronous method method GetStringAsync, but the method is performed synchronously Here are specific reasons will be explained below), in the Start()prior method is finished, the entire program is blocked. The asynchronous programming can solve this problem, a simple phrase to summarize asynchronous programming is that the program in accordance with the code without having to perform a top-down order .

async/await

C # 5.0 adds async and await keywords, use these keywords can greatly simplify asynchronous programming

Use  async  keyword method, can be a lambda expression or anonymous method is marked as asynchronous, that is, the method should contain one or more await expression, but async keyword itself does not create an asynchronous operation.

async Task Asy public () 
{ 
  // do something ... 
} 
It should be noted that, if the method of using async keyword tag is not used await keyword (the compiler will give a warning but not an error), then the method will They will perform in a synchronized manner.

Some asynchronous method defined requirements

The definition of an asynchronous method should meet the following points:

  • Use the keyword async method to modify
  • Use the keyword await an asynchronous method (without using the compiler will give a warning but not an error), or asynchronous method performs a synchronized manner
  • Try not to use a void return type. If you want an asynchronous method returns a void type, use the Task
  • Async asynchronous method names ending
  • Asynchronous methods can not declare a variable using the ref or out keyword modified

An asynchronous method defined below StartAsync():

static async Task<int> StartAsync()
{
    HttpClient client = new HttpClient();
    var str = await client.GetStringAsync("https://www.visualstudio.com/");
    return str.Length;
}

 

Asynchronous method return type

  • Task <T>
    If you use when calling the anonymous method await keywords and returns an anonymous method of type Task <T>, then we get the return type is T. Failure to use the await keyword, type Task is returned. Unused await, when you call the method result is GetStringAsync Task type.

  

From the graph we can see that await keyword is not used when calling GetStringAsync method, result is the Task type, we can get detailed information about the type of result by GetType () method:

From the map you can see the full name is the result of type System.Threading.Tasks.Task

 


 

When we can see from the chart using the await keyword, result is a string type, and an anonymous method GetStringAsync return type is Task <string>

  • Task
    if used when calling the anonymous method await keywords, and anonymous method's return type is Task, then we get a return type of void. If using await keyword, then get a return type of Task.

  • void
    is not recommended as an asynchronous void the return value.
    Because the use of Task or Task <TResult> task as a return value, its properties carry information about its status and history, such as whether the task is completed, the asynchronous method is causing abnormal or canceled and what the final results are. Await the operator can access these properties.

Asynchronous method execution flow


Asynchronous program execution flow

 

 

Guess you like

Origin www.cnblogs.com/youmingkuang/p/11281870.html