一个简单的await使用例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    public class Program
    {
        static  int Main(string[] args)
        {
            Console.WriteLine("start...");
            Test();
            Thread.Sleep(15000);
            Console.WriteLine("end...");
            return 0;
        }
 

        async static void Test()
        {
            Console.WriteLine("Begin Test");
            for (int i = 0; i < 3; i++)
            {
                await TestTask(i);
            }
            Console.WriteLine("End Test");
        }
        static Task TestTask(int num)
        {
            return Task.Run(() =>
            {
                Console.WriteLine("Begin Task:" + num.ToString());
                Thread.Sleep(5000);
                Console.WriteLine("End Task:" + num.ToString());
            });
        }
    }
}

image

posted @ 2022-02-09 15:11  zhaogaojian  阅读(117)  评论(0编辑  收藏  举报