Form 界面假死

yakune 2010-11-08 06:35:30
程序点击按钮执行循环的时候界面是假死状态,
循环方法:

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();
}
...全文
209 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
alangmcj 2010-11-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lovlc 的回复:]

引用 6 楼 ywj157 的回复:

private void button3_Click(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(this.sour));
th.Start();
}
红色字体的改成listBox1.BeginInvoke.....................

第一行就……
[/Quote]
报什么错误?
soolx 2010-11-09
  • 打赏
  • 举报
回复
顶起来吧.
xrongzhen 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lovlc 的回复:]
引用 6 楼 ywj157 的回复:

private void button3_Click(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(this.sour));
th.Start();
}
红色字体的改成listBox1.BeginInvoke.....................

第一行就报……
[/Quote]

第一行报什么错? 你不说谁知道啊 语法没啥错
龍月 2010-11-08
  • 打赏
  • 举报
回复
看下 backgroundworker 控件 此消息通过 【CSDN论坛 Winform测试版】 回复! 龙月.NET的博客
兔子-顾问 2010-11-08
  • 打赏
  • 举报
回复
try

linkLabel3_LinkClicked(null, null); //下一页

改为

Application.DoEvents();
linkLabel3_LinkClicked(null, null); //下一页
yakune 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ywj157 的回复:]

private void button3_Click(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(this.sour));
th.Start();
}
红色字体的改成listBox1.BeginInvoke.....................
[/Quote]
第一行就报错了,改下面的有用吗??
哥子谭 2010-11-08
  • 打赏
  • 举报
回复
这个可能是你的数据多了或者是程序出现死循环了
ywj157 2010-11-08
  • 打赏
  • 举报
回复
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 );
}
}
}
[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.....................
yakune 2010-11-08
  • 打赏
  • 举报
回复
这样调用异常:指定的转换无效。 在[HtmlElementCollection collectionoo = extendWebBrowser1.Document.GetElementsByTagName("A");]

private void button4_Click(object sender, EventArgs e)
{
AsyncEventHandler asy = new AsyncEventHandler(this.sour);
IAsyncResult ia = asy.BeginInvoke(null, null);
//sour();
}
freeboy827 2010-11-08
  • 打赏
  • 举报
回复
线程的方法不要调用 控件

111,129

社区成员

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

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

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