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);
        }
    }
}

 

posted @ 2019-11-24 15:51  FredGrit  阅读(262)  评论(0编辑  收藏  举报