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);
} }
这是我做的 监控网站标题的代码 用到线程 可每次执行线程的时候程序就会卡 类似于假死...谁能教教我怎么做才不会出现假死..谢谢了
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);
} }
这是我做的 监控网站标题的代码 用到线程 可每次执行线程的时候程序就会卡 类似于假死...谁能教教我怎么做才不会出现假死..谢谢了
Thread newThreadi = new Thread(proc);
newThreadi.Start();
newThreadi.IsBackground = true;
你去JOIN他干嘛。。Join等待他执行完毕,你既然在等这个线程执行完毕了,那不就变成同步执行了,既然如此那还开线程干什么,等于在一个线程上执行啊,把这句去掉
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()
{
//
}
功能分开.
{
rows = this.listView1.Items.Count;
int i = 0;
while (i < rows)
{
DownLoad();
Thread.Sleep(1000);
}
}