c#-线程-Task.WhenAll

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

namespace ConsoleApp5
{
    class Program
    {
        public static int num = 0;
        static void Main(string[] args)
        {
            Main2();
            System.Console.WriteLine("hello word");
            System.Console.ReadKey();
        }
        static async void Main2()
        {

            var t1 = new Task(fun);
            t1.Start();
            var t2 = new Task(fun);
            t2.Start();
            await Task.WhenAll(t1, t2);
            System.Console.WriteLine(num);
        }
        public static void fun() {
            for (int i = 0; i < 10; i++) {
                Thread.Sleep(10);
                num++;
            }
        }
    }
}

运行结果 

c#-线程-Task.WhenAll_第1张图片

你可能感兴趣的:(c#-多线程)