所有线程结束后如何调用某些函数? [英] How Can I Call Some Function After All Threads End?

查看:167
本文介绍了所有线程结束后如何调用某些函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我用C#编写了一个多线程应用程序。它运行良好,但是我想在所有线程结束后调用一些函数。我使用了join(),但它冻结了我的表单。我该怎么办?

我将非常感谢你的帮助。

非常感谢。

Hi,
I wrote An multi-thread application by C#.it works well but i want to call some function after all threads end. i used join() but it freezes my form. what can i do?
I would appreciate your help.
thanks a lot.

private void BtnBFS_Click(object sender, EventArgs e)
{
Thread[] SpiderThread = new Thread[n];
object[] Param = new object[n];
ThreadCount = n;

for (int i = 0; i < n; i++)
  Param[i] = TxtURL.Lines[i].ToString();

for (int Index = 0; Index < n; Index++)
{
  SpiderThread[Index] = new Thread(BFS);
  SpiderThread[Index].Name = Index.ToString();
}

for (int i = 0; i < n; i++)
  SpiderThread[i].Start(Param[i]);

// i want call some functions here
}

推荐答案

我将它从使用线程更改为 BackgroundWorker [ ^ ] - 这很简单,但它增加了有用的功能,如进度报告和事件告诉你在线程完成时。

如果您处理 BackgroundWorker.RunWorkerCompleted事件 [ ^ ]你可以勾选你的清单,一切都消失了 - 继续。



那个方式,你的UI线程没有等待其他线程完成,并保持响应。
I'd change it from using a Thread to a BackgroundWorker[^] - it's pretty simple to do, but it adds useful functionality like progress reporting and events which tell you when the thread completes.
If you handle the BackgroundWorker.RunWorkerCompleted Event[^] you could "tick it off" your list and when all are gone - continue.

That way, your UI thread isn't sitting waiting for the other threads to finish, and remains responsive.


这篇关于所有线程结束后如何调用某些函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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