如何等待线程使用.NET完成?在C#中,我从来没有真正使用过线程,在C#中,我需要有两个线程,以及主UI线程。基本上,我有以下几点。public void StartTheActions(){
//Starting thread 1....
Thread t1 = new Thread(new ThreadStart(action1));
t1.Start();
// Now, I want for the main thread (which is calling `StartTheActions` method)
// to wait for `t1` to finish. I've created an event in `action1` for this.
// The I wish `t2` to start...
Thread t2 = new Thread(new ThreadStart(action2));
t2.Start();}所以,本质上,我的问题是如何让一个线程等待另一个线程完成。做这件事最好的方法是什么?
3 回答
慕村9548890
TA贡献1884条经验 获得超4个赞
public void StartTheActions(){ ManualResetEvent syncEvent = new ManualResetEvent(false); Thread t1 = new Thread( () => { // Do some work... syncEvent.Set(); } ); t1.Start(); Thread t2 = new Thread( () => { syncEvent.WaitOne(); // Do some work... } ); t2.Start();}
- 3 回答
- 0 关注
- 475 浏览
添加回答
举报
0/150
提交
取消