使用类库中的WebBrowser控件
我试图在类库中使用此控件,但是当我运行下面的代码时,我没有看到向 google 发送的请求(使用 fiddler)。
public class WebBrowserTest
{
public WebBrowserTest()
{
var t = new Thread(StartBrowser);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private void StartBrowser()
{
WebBrowser web;
web = new WebBrowser();
web.Navigate("http://www.google.com");
}
}
我的猜测是这与线程有关,并且可能是线程在控件有机会发送请求之前结束。但我不知道从哪里开始解决这个问题。
解决方案
我发现这个解决方案有效,事件被触发并且主线程等待 STA 线程。
public class WebThread
{
private WebBrowser web { get; set; }
public void StartBrowser()
{
web = new WebBrowser();
web.Visible = true;
web.DocumentCompleted += Web_DocumentCompleted;
web.ScriptErrorsSuppressed = true;
web.Navigate("http://www.google.com");
Application.Run();
web.Dispose();
}
private void Web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Debug.Print("Arrived: " + e.Url.ToString());
if (e.Url.ToString() == "http://www.google.com.au/")
{
Application.ExitThread();
}
}
}
public class WebBrowserTest
{
public WebBrowserTest()
{
Debug.Print("Thread is starting.");
var webThread = new WebThread();
var t = new Thread(webThread.StartBrowser);
t.SetApartmentState(ApartmentState.STA);
t.Start();
while(t.IsAlive)
{
Thread.Sleep(5000);
}
Debug.Print("Thread has finished.");
}
}
I'm trying to use this control inside a class library, but when I run the code below, I did not see a request to google being sent (using fiddler).
public class WebBrowserTest
{
public WebBrowserTest()
{
var t = new Thread(StartBrowser);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private void StartBrowser()
{
WebBrowser web;
web = new WebBrowser();
web.Navigate("http://www.google.com");
}
}
My guess is this has something to do with threading, and possibly the thread ending before the control gets a chance to send the request. But I have no idea where to start with solving this.
THE SOLUTION
I found this solution to work, the events are getting fired and the main thread waits for the STA thread.
public class WebThread
{
private WebBrowser web { get; set; }
public void StartBrowser()
{
web = new WebBrowser();
web.Visible = true;
web.DocumentCompleted += Web_DocumentCompleted;
web.ScriptErrorsSuppressed = true;
web.Navigate("http://www.google.com");
Application.Run();
web.Dispose();
}
private void Web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Debug.Print("Arrived: " + e.Url.ToString());
if (e.Url.ToString() == "http://www.google.com.au/")
{
Application.ExitThread();
}
}
}
public class WebBrowserTest
{
public WebBrowserTest()
{
Debug.Print("Thread is starting.");
var webThread = new WebThread();
var t = new Thread(webThread.StartBrowser);
t.SetApartmentState(ApartmentState.STA);
t.Start();
while(t.IsAlive)
{
Thread.Sleep(5000);
}
Debug.Print("Thread has finished.");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WebBrowser.Navigate( ... )
不会阻塞 - 它会在发送请求之前立即返回。由于您的线程函数随后退出,因此整个线程结束并接管您的WebBrowser
控制权。如果您只是想下载网页,请查看 WebClient 类。它有许多异步方法,这意味着您可能甚至不必创建自己的线程。
WebBrowser.Navigate( ... )
doesn't block - it returns immediately, before the request is sent. Since your thread function then exits, your whole thread ends and takes yourWebBrowser
control with it.If you're just trying to download a web page, have a look at the WebClient class. It has many async methods which means you probably won't even have to create your own thread.