等待两个线程完成 [英] Wait for two threads to finish
问题描述
如果您有启动其他两个线程一个主线程。
是什么使主线程等待其他两个线程干净的方式?
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)。
- 这两个产生的线程应该能够报告异常和返回结果值
推荐答案
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屋!