程序点击按钮执行循环的时候界面是假死状态,
循环方法:private void sour()
{
HtmlElementCollection collectionoo = extendWebBrowser1.Document.GetElementsByTagName("A"); foreach (HtmlElement elem in collectionoo)
{
if (elem.OuterHtml.Contains("title"))
{
if (!elem.OuterHtml.Contains("d_link d_a"))
{
listBox1.Items.Add(elem.InnerText);
string ostr = elem.OuterHtml;
string ostr1 = ostr.Substring(ostr.LastIndexOf("http"));
string ostr2 = ostr1.Substring(0, ostr1.LastIndexOf("\" target"));
HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(ostr2);
WebResponse resp = myWebRequest.GetResponse();
StreamReader oStreamRd = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("GB2312"));
string oStr3 = oStreamRd.ReadToEnd();
string oStr4 = oStr3.Substring(oStr3.LastIndexOf("surl+\"") + 6);
string oStr5 = oStr4.Substring(0, oStr4.LastIndexOf("?\" + Au"));
listBox1.Items.Add(oStr5 );
}
}
}
linkLabel3_LinkClicked(null, null); //下一页
}
调用方法://这样调用,只有循环结束界面才显示数据.
private void button4_Click(object sender, EventArgs e)
{
sour();
}
//用线程调用,异常: 指定的转换无效。在[HtmlElementCollection collectionoo = extendWebBrowser1.Document.GetElementsByTagName("A");]
private void button3_Click(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(this.sour));
th.Start();
}
循环方法:private void sour()
{
HtmlElementCollection collectionoo = extendWebBrowser1.Document.GetElementsByTagName("A"); foreach (HtmlElement elem in collectionoo)
{
if (elem.OuterHtml.Contains("title"))
{
if (!elem.OuterHtml.Contains("d_link d_a"))
{
listBox1.Items.Add(elem.InnerText);
string ostr = elem.OuterHtml;
string ostr1 = ostr.Substring(ostr.LastIndexOf("http"));
string ostr2 = ostr1.Substring(0, ostr1.LastIndexOf("\" target"));
HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(ostr2);
WebResponse resp = myWebRequest.GetResponse();
StreamReader oStreamRd = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("GB2312"));
string oStr3 = oStreamRd.ReadToEnd();
string oStr4 = oStr3.Substring(oStr3.LastIndexOf("surl+\"") + 6);
string oStr5 = oStr4.Substring(0, oStr4.LastIndexOf("?\" + Au"));
listBox1.Items.Add(oStr5 );
}
}
}
linkLabel3_LinkClicked(null, null); //下一页
}
调用方法://这样调用,只有循环结束界面才显示数据.
private void button4_Click(object sender, EventArgs e)
{
sour();
}
//用线程调用,异常: 指定的转换无效。在[HtmlElementCollection collectionoo = extendWebBrowser1.Document.GetElementsByTagName("A");]
private void button3_Click(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(this.sour));
th.Start();
}
{
AsyncEventHandler asy = new AsyncEventHandler(this.sour);
IAsyncResult ia = asy.BeginInvoke(null, null);
//sour();
}
{
HtmlElementCollection collectionoo = extendWebBrowser1.Document.GetElementsByTagName("A"); foreach (HtmlElement elem in collectionoo)
{
if (elem.OuterHtml.Contains("title"))
{
if (!elem.OuterHtml.Contains("d_link d_a"))
{
listBox1.Items.Add(elem.InnerText);
string ostr = elem.OuterHtml;
string ostr1 = ostr.Substring(ostr.LastIndexOf("http"));
string ostr2 = ostr1.Substring(0, ostr1.LastIndexOf("\" target"));
HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(ostr2);
WebResponse resp = myWebRequest.GetResponse();
StreamReader oStreamRd = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("GB2312"));
string oStr3 = oStreamRd.ReadToEnd();
string oStr4 = oStr3.Substring(oStr3.LastIndexOf("surl+\"") + 6);
string oStr5 = oStr4.Substring(0, oStr4.LastIndexOf("?\" + Au"));
listBox1.Items.Add(oStr5 );
}
}
}
[color=#800000]linkLabel3_LinkClicked(null, null); //下一页[/color]
}
private void button3_Click(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(this.sour));
th.Start();
}
红色字体的改成listBox1.BeginInvoke.....................
linkLabel3_LinkClicked(null, null); //下一页
此消息通过 【CSDN论坛 Winform测试版】 回复!
龙月.NET的博客