C# 多執行緒之新執行緒延時執行
阿新 • • 發佈:2019-01-29
using System; using System.Threading; namespace ConsoleApplication1 { class Program { public static ManualResetEvent mre = new ManualResetEvent(false); static void Main(string[] args) { Thread newThread = new Thread(new ThreadStart(NewThread)); newThread.Name = "新執行緒"; newThread.Start(); mre.Set(); Console.WriteLine("Main 函式結束。"); } static void NewThread() { Console.WriteLine("新執行緒: waiting for an event"); mre.WaitOne(); Console.WriteLine("新執行緒: got an event"); Console.WriteLine(DateTime.Now); Thread.Sleep(2000); //延時兩秒 Console.WriteLine(DateTime.Now); Console.WriteLine("新執行緒已執行完。。"); Console.WriteLine(); } } }
Result: