vs 简单线程打印倒计时

//VS新建控制台程序

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

namespace ConsoleApp3
{
class Program
{

    static void Main(string[] args)
    {
        Func<int, string, string> wait = Wait10;
        IAsyncResult ar = wait.BeginInvoke(10, "等待了10s", null, null);
        while (ar.IsCompleted == false)//线程是否被完成
        {
            Console.Write("");
        }

        string res = wait.EndInvoke(ar);
        Console.WriteLine(res);
        Console.ReadKey();//弹窗暂停
    }


    static string Wait10(int a, string b)
    {
        for (int i = a; i > 0; i--)
        {
            Thread.Sleep(1000);  //睡眠多长时间 ms
            Console.WriteLine(i);
        }
        Thread.Sleep(1000);//第十一秒
       // Console.WriteLine("等待了10s");
        return b;
    }

}
}

猜你喜欢

转载自blog.csdn.net/qq_42986916/article/details/84189390
今日推荐