线程Join()方法:让一个线程等待另一线程终结后再继续运行。
- private static void Method()
- {
- Thread.Sleep(5000);
- Console.WriteLine("当前线程:" + Thread.CurrentThread.Name);
- }
- static void Main(string[] args)
- {
- Thread.CurrentThread.Name = "MainThread";
- Thread thread = new Thread(Method);
- thread.Name = "Thread";
- thread.Start();
- //会阻止主线程,直到thread线程终结(线程方法返回或线程遇到异常)
- //输出:当前线程:Thread
- // 主线程:MainThread
- //可以注销此句对比输出结果
- thread.Join();
- Console.WriteLine("主线程:" + Thread.CurrentThread.Name);
- Console.Read();
- }