C# await async异步编程简单理解

async 异步方法

await 执行异步调用的位置

await Method1();    // 同步执行,先完成Method1,在执行Method2
//Method1();          // 异步执行,Method1、Method2同时执行
Method2();
Console.ReadKey();


static async Task Method1()
{
    await Task.Run(() =>
    {
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine("Method1.");
            Task.Delay(100);
        }
    });
}

static void Method2()
{
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine("Method2.");
        Task.Delay(100);
    }
}

 

posted on 2022-10-18 17:07  xzj19870125  阅读(25)  评论(0编辑  收藏  举报

导航