Asynchronous programming 1

Base

  1. keyword async await
  2. The return value of an asynchronous method is Task<T>, if there is no return value, use Task instead of void
  3. Call: await asynchronous method (parameter)
  4. Get T in the return value: T (type passed in by generics) identifier=await asynchronous method (parameter)
  5. The await asynchronous method appears in the method, the method must be decorated with async

case

using System;
using System.IO;
using System.Threading.Tasks;

namespace TaskAysnc
{
    
    
    class Program
    {
    
    
        //方法内部调用异步方法 使用async修饰方法
        async static Task Main(string[] args)
        {
    
    

            string fileName = @"D:\Program Files (x86)\OneDrive\桌面\异步\1.txt";

            await File.WriteAllTextAsync(fileName, "异步写入");//调用异步方法

		   //ReadAllTextAsync 返回值为Task<string> 获取string
            string result = await File.ReadAllTextAsync(fileName);//获取异步方法的返回值

            Console.WriteLine(result);
        }
    }
}

Guess you like

Origin blog.csdn.net/weixin_43796392/article/details/122590732