求教线程问题!!!急!~

qifenbamiao 2010-01-20 04:45:02
rows = this.listView1.Items.Count;
int i = 0;
while (i < rows)
{
str = listView1.Items[i].SubItems[0].Text; //str是网站的地址
str2 = listView1.Items[i].SubItems[1].Text; //str2是监控码 也就是网站的标题<title>
i++;
Thread newThreadi = new Thread(new ThreadStart(proc));
newThreadi.Start();
newThreadi.IsBackground = true;
newThreadi.Join();
}




public void proc()
{
WebClient web = new WebClient();
byte[] data = web.DownloadData(str); //获取网站源代码
string res = Encoding.Default.GetString(data);
web.Dispose();
int loc1 = res.IndexOf("<title>");
int loc2 = res.IndexOf("</title>");
title = res.Substring(loc1 + 7, loc2 - (loc1 + 7)).ToString(); //获得网站标题
if (title.ToString()!=str2.ToString())
{
this.notifyIcon1.ShowBalloonTip(1, "温馨提示", "打开" + str2 + "页面时出现异常,请联系13523859270", ToolTipIcon.Error);

}

}
这是我做的 监控网站标题的代码 用到线程 可每次执行线程的时候程序就会卡 类似于假死...谁能教教我怎么做才不会出现假死..谢谢了
...全文
69 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
evjen 2010-01-20
  • 打赏
  • 举报
回复


private void Do()
{
rows = this.listView1.Items.Count;
int i = 0;
while (i < rows)
{
DownLoad();
Thread.Sleep(1000);
}
}
evjen 2010-01-20
  • 打赏
  • 举报
回复
建议楼主这样


public void Start()
{
Thread thread = new Thread(new ThreadStart(Do));
thread.Start();
}

private void Do()
{
rows = this.listView1.Items.Count;
int i = 0;
while (i < rows)
{
DownLoad();
}
}

private void DownLoad()
{
//
}


功能分开.
ckl881003 2010-01-20
  • 打赏
  • 举报
回复
newThreadi.Join();
你去JOIN他干嘛。。Join等待他执行完毕,你既然在等这个线程执行完毕了,那不就变成同步执行了,既然如此那还开线程干什么,等于在一个线程上执行啊,把这句去掉
_autotest 2010-01-20
  • 打赏
  • 举报
回复
习惯这样用。
Thread newThreadi = new Thread(proc);
newThreadi.Start();
newThreadi.IsBackground = true;
Hamsic 2010-01-20
  • 打赏
  • 举报
回复
-.- 用委托把。。

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧