如何在多线程应用程序中防止冻结表单? [英] How can I prevent Freezing Form in Multi thread Application?

查看:70
本文介绍了如何在多线程应用程序中防止冻结表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我编写了一个多线程应用程序,用于通过C#从网页(例如在线社交网络)中提取朋友列表。不幸的是My Form挂起来并且没有回应任何事情。(冻结)我该怎么办?

我将非常感谢你的帮助。

非常感谢。



我的代码是:

Hi,
I wrote An multi-thread application to extract list of friends from a web page such as an online social network by C#. unfortunately My Form hangs and dont response to anything.(freezing) what could i do?
I would appreciate your help.
thanks a lot.

my Code is:

/.................................................................
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]);
for (int i = 0; i < n n; i++)
SpiderThread[i].Join();
}
/////............................................................
public void BFS(object Param)
{
try
{
define some variable
while (UnCrawledQueue.Count >= 0 and visited <= NumC)
{
main process;
}
}
}

推荐答案

删除 SpiderThread [i] .Join(); 行。当你在一个线程上调用 Join 时,它会阻塞调用线程(在你的情况下:UI线程),直到线程完成它的工作。

Remove the SpiderThread[i].Join(); line. When you call Join on a thread it blocks the calling thread (in your case: the UI thread) until the thread has finished its work.


这篇关于如何在多线程应用程序中防止冻结表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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