日期:2014-05-19  浏览次数:20903 次

C#中线程问题?
问题:
1C#中可谓当前线程?何谓主线程?当前线程一共有几个呀?

2Thread   thread   =   new   thread(new   threadStart(event));
thread.start();
thread.join();//等待线程结束

我就是怎么也不明白thread.join()\是什么作用.
请高手回答,谢谢!

------解决方案--------------------
1C#中可谓当前线程?何谓主线程?当前线程一共有几个呀?
______________________________________________________

当前线程: System.Threading.Thread.CurrentThread, 是你目前代码执行的线程.

主线程: 在不同程序中有不同的含义, 最常见的Form程序,主线程就是启动Mian的那个UI线程.

当前线程一共有几个呀: 一个,就是指你代码目前执行的线程.它可以是主线程,也可以是你用new thread () 生成的新线程.
------解决方案--------------------
Thread thread = new thread(new threadStart(event));
thread.start();
thread.join();//等待线程结束
===================================
阻塞当前线程thread.必须等thread线程运行完毕之后,才会运行其他线程;

MSDN:阻塞调用线程,直到某个线程终止时为止。
------解决方案--------------------
thread.join()\是什么作用.
___________________________

当有某种需要,要求代码等待thread线程结束的时候用的. 比如thread处理一些数据,你需要等处理结束后才进行其它工作,这时候就可以用thread.join()等待.

如果此thread还没有结束,程序就停在这句话上边等, 直到它结束为止.