在winform(C#)下的一个小应用程序(部分代码): //按钮单击事件
private void btn_SimulationClick_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(LoadPageByProxy));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
} public void LoadPageByProxy()
{
string mainKey="sss";
string aboutKey="dddd";
System.Windows.Forms.WebBrowser webbr = new System.Windows.Forms.WebBrowser();
webbr.Navigate("http://www.xx.net/request.aspx?bs=" + mainKey + "&wd=" + aboutKey);
webbr.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webRequsted);
} public void webRequsted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
MessageBox.Show("加载成功!");
}在没有启用线程的时候,DocumentCompleted是好像是可以触发的,但是采用纯程过后,DocumentCompleted就不起作用了!不知道为什么,请各位老兄指点!
我所要实现的功能是,想实现多线程去请求页面!每一个线程,都循环更换不同mainKey,aboutKey加载页面的。并且都要把每一个线程,循环加载页面的成功结束显示出来。
以上仅为部分代码,没有写出循环更换不同的mainKey,aboutKey……
private void btn_SimulationClick_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(LoadPageByProxy));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
} public void LoadPageByProxy()
{
string mainKey="sss";
string aboutKey="dddd";
System.Windows.Forms.WebBrowser webbr = new System.Windows.Forms.WebBrowser();
webbr.Navigate("http://www.xx.net/request.aspx?bs=" + mainKey + "&wd=" + aboutKey);
webbr.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webRequsted);
} public void webRequsted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
MessageBox.Show("加载成功!");
}在没有启用线程的时候,DocumentCompleted是好像是可以触发的,但是采用纯程过后,DocumentCompleted就不起作用了!不知道为什么,请各位老兄指点!
我所要实现的功能是,想实现多线程去请求页面!每一个线程,都循环更换不同mainKey,aboutKey加载页面的。并且都要把每一个线程,循环加载页面的成功结束显示出来。
以上仅为部分代码,没有写出循环更换不同的mainKey,aboutKey……
你注意了没有,如果是这情况就需要使用委托
webbr.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webRequsted);
thread.SetApartmentState(ApartmentState.STA);
documentcompletele事件绑定要在navigate之前。
将C:\Program Files\Microsoft.NET\Primary Interop Assemblies目录中的Microsoft.mshtml.dll考备到应用程序目录下就OK了。
回答的有点晚,还是希望对其他遇到类似问题的朋友们有帮助。