如何使用Web浏览器对象在后台工作在C# [英] How to use web browser object in background worker in C#
本文介绍了如何使用Web浏览器对象在后台工作在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有低于code问题。有没有人告诉它为什么不工作的BackgroundWorker,我怎么能解决这个问题。
字符串地址=我的网址
webBrowser.Navigate(新的URI(地址));
做
{
Application.DoEvents();
}而(webBrowser.ReadyState = WebBrowserReadyState.Complete!);
解决方案
没有!
您更好地打开一个新的线程,并从那里指示web浏览器
Application.DoEvents()
是有点儿邪恶。
下面是如何开始
System.Threading.Thread T =新System.Threading.Thread(()=>
{
yourWebBrowser.Navigate(http://Google.com);
});t.ApartmentState = System.Threading.ApartmentState.STA;
t.Start();
要获得通知,该页面已被加载,你可以订阅DocumentCompleted事件像这样:
yourWebBrowser.DocumentCompleted + = WebBrowserDocumentCompleted;无效WebBrowserDocumentCompleted(对象发件人,System.Windows.Forms.WebBrowserDocumentCompletedEventArgs E)
{
抛出新NotImplementedException();
}
I have problem with code below. Can some one tell why it doesn't work in BackgroundWorker and how can i solve this problem
string address = "My URL";
webBrowser.Navigate(new Uri(address));
do
{
Application.DoEvents();
} while (webBrowser.ReadyState != WebBrowserReadyState.Complete);
解决方案
No!!
You better open a new thread and instruct the WebBrowser from there
Application.DoEvents()
is kinda evil.
Here is how you can start
System.Threading.Thread t = new System.Threading.Thread(() =>
{
yourWebBrowser.Navigate("http://Google.com");
});
t.ApartmentState = System.Threading.ApartmentState.STA;
t.Start();
To get notified that the page has been loaded you can subscribe to the DocumentCompleted event as so:
yourWebBrowser.DocumentCompleted += WebBrowserDocumentCompleted;
void WebBrowserDocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
throw new NotImplementedException();
}
这篇关于如何使用Web浏览器对象在后台工作在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文