vs 簡單執行緒列印倒計時
阿新 • • 發佈:2018-11-26
//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; }
}
}