等待两个线程完成 [英] Wait for two threads to finish

查看:105
本文介绍了等待两个线程完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您有启动其他两个线程一个主线程。
是什么使主线程等待其他两个线程干净的方式?

If you have one main thread that starts two other threads. what is the cleanest way to make the primary thread wait for the two other threads?

我可以使用bgndworker和检查两个BGND工人的睡眠微调。IsBusy,但我认为有一个更好的办法

I could use bgndworker and sleep spinner that checks for both the bgnd workers's IsBusy, but I would think there's a better way.

修改
有更高的要求:

EDIT Some more requirements:


  • 主线程有一些其他的工作要做(如GUI)。

  • 这两个产生的线程应该能够报告异常和返回结果值

推荐答案

请参阅上的此线程。我喜欢此选项 ;-p

See the answers on this thread. I like this option ;-p

Forker p = new Forker();
p.Fork(delegate { DoSomeWork(); });
p.Fork(delegate { DoSomeOtherWork(); });
p.Join();

重新返回值/报告例外​​ - 只是让每个叉做到这一点作为的结束回调逻辑...(可以使用捕捉变量传递状态到两个叉,其中包括一个共享记录仪等)。

Re returning values / reporting exceptions - just have each fork do that as a callback at the end of the logic... (you can use captured variables to pass state into both the forks, including a shared logger etc).

这篇关于等待两个线程完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆