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!\n\n");
            }
        }

        private static void PrintTime(int i)
        {
            Console.WriteLine($"i is {i},Now is {DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
            Thread.Sleep(300);
        }
    }
}

 

你可能感兴趣的:(C# loop executed one by one wait the former completed)