C# 同时运行多个 Task

        public static async Task<int> CalcAsync(int wait, int shit)
        {
            await Task.Run(() => Thread.Sleep(wait));
            return shit;
        }
        public static async void TestDoubleTask()
        {

            Task<int> t1 = CalcAsync(1000, 1);  // t1 开始运行
            Task<int> t2 = CalcAsync(2000, 2);  // t2 开始运行
            int r1 = await t1;
            int r2 = await t2;                  // 等待 t1, t2 均完成
            Console.WriteLine(r1);
            Console.WriteLine(r2);
        }

        public static void Main()
        {
            TestDoubleTask();
            Console.ReadKey();
        }

你可能感兴趣的:(C# 同时运行多个 Task)