如何确保所有子线程完成? [英] How to be sure all sub thread finished?

查看:68
本文介绍了如何确保所有子线程完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有这样的功能

Hi all,
I have a function like that

// initial some thread
Thread td1 = new ...
Thread td2 = new ...
Thread td3 = new ...

// start those thread
td1.Start();
td2.Start();
td3.Start();

// wait for all thread finished.
???? => what is solution for this?

// this action have to wait all threads finish before be started.
doNextAction();



您能告诉我如何确保所有子线程完成吗?
非常感谢,
TuanNM



Could you show me how to be sure all child thread finished?
Thank so much,
TuanNM

推荐答案

请参见 Thread.Join [ ^ ].

在您的示例中:
See Thread.Join[^].

In your example:
td1.Join();
td2.Join();
td3.Join();


您可以使用Thread.Join,但是它将一直阻塞,直到线程确实完成为止.
您可以轮询Thread.IsAlive,但这也不是一个好主意.
我将拥有一个ThreadFinished事件处理程序,在其中检查了所有三个线程的IsAlive属性.完成所有操作后,您可以继续.
You could use Thread.Join, but that will block until the thread is indeed finished.
You could poll Thread.IsAlive, but that isn''t a brilliant idea either.
I would have a ThreadFinished event handler, in which I checked all three threads IsAlive property. When they are all done, you can continue.


这篇关于如何确保所有子线程完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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