Thread.Join() 需要帮助
这是我的场景。
private void Form1_Load(object sender, EventArgs e)
{
List<tasks> listOfTasks = GetListOfTasks()
foreach(Task task in Tasks)
{
DoSomeWork work = new DoSomeWork();
Thread workerThread = new Thread(work.CompleteTask());
workerThread.Start();
}
***How to determine if all threads have finished the task?***
}
我想到使用CurrentThread.join()。但当前线程将是form_load()。所以这是行不通的。
关于如何确定所有线程是否完成其任务的任何建议。
提前致谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:好的,现在我们有了更多信息,并且知道问题实际上并不是关于用户界面的,这很简单:
当然,使用 TPL 的
WaitForAll
这是一个比这更好的主意 - 如果你可以使用它,就这样做。但如果您使用普通线程,Join
就可以了。编辑:我一直假设您正在编写一个基于 Form1_Load 方法的 Windows 窗体应用程序。如果您能在问题中提供更多背景信息,将会很有帮助。
嗯,您的线程不太可能在启动所有线程后立即完成...并且您不应该阻塞 UI 线程等待它们完成,因为您的 UI 将挂起。
相反,您应该在每个
DoSomeWork
任务完成时回调 UI。 DoSomeWork 可以有一个事件,您可以向其添加处理程序,或者您可以将完成处理程序委托传递到构造函数中,或者它可能直接了解表单并可以回调自身 - 这取决于什么它确实在做。无论如何,当任务有效完成后,您可以使用
Control.BeginInvoke
回发到 UI 线程。该表单可以跟踪已完成的任务数量,因此,如果它需要在一切完成后执行某些操作,它就可以这样做。如果这没有帮助,请提供更多信息,说明所有任务完成后您希望发生什么以及这些任务正在做什么。
EDIT: Okay, now we have more information and know that the question isn't really about a user interface, it's easy:
Of course, using the TPL's
WaitForAll
is a better idea than this - if you can use it, do so. But if you're using plain threads,Join
is fine.EDIT: I've been assuming you're writing a Windows Forms application, based on the Form1_Load method. It would be helpful if you could give more context in the question.
Well, your threads are unlikely to have all finished immediately after you've started them all... and you shouldn't block the UI thread waiting for them to finish, as your UI will hang.
Instead, you should make each
DoSomeWork
task call back into the UI when it's finished. EitherDoSomeWork
could have an event you could add a handler to, or you could pass a completion handler delegate into the constructor, or perhaps it knows about the form directly and can call back itself - it depends on what it's doing really.Anyway, when the task has effectively finished, you can post back to the UI thread using
Control.BeginInvoke
. The form could keep track of how many tasks has finished, so that if it needs to do something when everything has finished, it can do so.If this doesn't help, please give more information about what you want to happen when all the tasks have finished, and what those tasks are doing.
如果您使用 .NET 4.0,为什么不利用 TPL:
此外,如果这是 ASP.NET,您最好使用 异步页面。
If you are using .NET 4.0 why not taking benefit of TPL:
Also if this is ASP.NET you are better off using asynchronous pages.
您可以使用
WaitHandle.WaitAll
等待多个句柄 - http://msdn.microsoft.com/en-us/library/z6w25xa6.aspx编辑 - 删除有关 UI 线程的评论 - 因为这是 asp.net 而不是 winforms...
You can use
WaitHandle.WaitAll
for waiting for multiple handles - http://msdn.microsoft.com/en-us/library/z6w25xa6.aspxEdit - removed comment about UI thread - since this is asp.net not winforms...