How to use web browser object in background worker in C#

落爺英雄遲暮 提交于 2019-12-23 06:08:06

问题


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);

回答1:


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();
}


来源:https://stackoverflow.com/questions/13579407/how-to-use-web-browser-object-in-background-worker-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!