C# loop executed one by one wait the former completed

强颜欢笑 提交于 2019-12-05 11:35:32
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);
        }
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!