欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# loop executed one by one wait the former completed

程序员文章站 2024-03-30 20:21:09
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ... ......
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);
        }
    }
}