您的位置:首页 > 其它

thread.Join(); 让主线程等待自己完成

2015-08-29 19:52 106 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
class Program
{
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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: