主题:多线程的问题
先看看我的代码:
private void btnLookhtml_Click(object sender, EventArgs e)
{
listSid.SelectedIndex = -1;
for (int i = 1; i <= listSid.Items.Count; i++)
{
listSid.SelectedIndex = listSid.SelectedIndex + 1;
string strlist =listSid.SelectedItem.ToString();
string strsid = strlist;
string rl;
WebRequest Request = WebRequest.Create(strsid);
WebResponse Response = Request.GetResponse();
Stream resStream = Response.GetResponseStream();
StreamReader sr = new StreamReader(resStream, Encoding.UTF8);
StringBuilder sb = new StringBuilder();
while ((rl = sr.ReadLine()) != null)
{
sb.Append(rl);
}
string strhtml = sb.ToString();//抓取得到的源网页
string he = strhtml.ToString();
richTextBox1.Text = stripHtml(he);//去除html标签后得到的源网页
Match TitleMatch = Regex.Match(he, "<title>([^<]*)</title>", RegexOptions.IgnoreCase | RegexOptions.Multiline);//获取网页的标题
我页面上有一个listbox 里面一行一个网址,然后循环截取网页的源码显示在richTextBox1里面。
但是我这种方法又慢又卡,求大侠在我代码里面做修改,用多线程使得我运行又快又好。谢谢 了
private void btnLookhtml_Click(object sender, EventArgs e)
{
listSid.SelectedIndex = -1;
for (int i = 1; i <= listSid.Items.Count; i++)
{
listSid.SelectedIndex = listSid.SelectedIndex + 1;
string strlist =listSid.SelectedItem.ToString();
string strsid = strlist;
string rl;
WebRequest Request = WebRequest.Create(strsid);
WebResponse Response = Request.GetResponse();
Stream resStream = Response.GetResponseStream();
StreamReader sr = new StreamReader(resStream, Encoding.UTF8);
StringBuilder sb = new StringBuilder();
while ((rl = sr.ReadLine()) != null)
{
sb.Append(rl);
}
string strhtml = sb.ToString();//抓取得到的源网页
string he = strhtml.ToString();
richTextBox1.Text = stripHtml(he);//去除html标签后得到的源网页
Match TitleMatch = Regex.Match(he, "<title>([^<]*)</title>", RegexOptions.IgnoreCase | RegexOptions.Multiline);//获取网页的标题
我页面上有一个listbox 里面一行一个网址,然后循环截取网页的源码显示在richTextBox1里面。
但是我这种方法又慢又卡,求大侠在我代码里面做修改,用多线程使得我运行又快又好。谢谢 了