C# loop executed one by one wait the former completed

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

namespace ConsoleApp393
{
    class Program
    {
        static void Main(string[] args)
        {
            LoopCompletedDemo();
            Console.ReadLine();
        }

        static void LoopCompletedDemo()
        {
            for(int i=0;i<10;i++)
            {
                Task task = Task.Run(() =>
                {
                    PrintTime(i);
                });
                task.Wait();
                Console.WriteLine($"Loop {i} completed!

");
            }
        }

        private static void PrintTime(int i)
        {
            Console.WriteLine($"i is {i},Now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
            Thread.Sleep(300);
        }
    }
}
原文地址:https://www.cnblogs.com/Fred1987/p/11922716.html