你如何通知父线程,所有的子线程已终止? [英] How do you notify a parent thread that all child threads have terminated?

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

问题描述

我有我移植到WPF一个控制台应用程序。该应用程序有3个工作线程,这都加入到主线程一些输出结果显示在屏幕上了。我的理解是,如果我尝试做同样的事情在WPF应用程序,图形用户界面将被阻塞并不会reponsive给用户。我怎么能通知家长线程所有的线程完成自己的工作?我认为解决方案将涉及委托和事件(或者BackgroundWorker的?),但它不是我清楚当线程终止如何获得调用的回调。



原始代码:

 的foreach(在对答线程t)
{
t.Start();
}

的foreach(在对答线程t)
{
t.Join();
}

//打印一些结果这里


解决方案

如果您正在使用三个的BackgroundWorker S,您可以使用事件 RunWorkerCompleted 的通知那一个工人完成:在启动工设置一个计数器来3然后再递减和 RunWorkerCompleted 检查调用该方法该计数器,如果它击中你完成0。


I have a console app that I'm porting to WPF. The application has 3 worker threads, that are all joined to the main thread before some output results are printed to the screen. My understanding is that, if I try and do the same thing in a WPF application, the GUI will be blocked and will not be reponsive to the user. How then can I notify the parent thread that all the threads have completed their work? I think the solution is going to involve delegates and events (or maybe BackgroundWorker?), but it was not clear to me how to get the callback invoked when the thread terminated.

Original Code:

foreach (Thread t in threadList)
{
                t.Start();
}

foreach (Thread t in threadList)
{
                t.Join();
}

// print some results here

解决方案

If you are using three BackgroundWorkers, you can use the event RunWorkerCompleted to notice that one of the workers is completed: Before starting the workers set a counter to 3 then decrement and check this counter in the method called by RunWorkerCompleted if it hits 0 you are finished.

这篇关于你如何通知父线程,所有的子线程已终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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