简体   繁体   English

如何在C#中的后台工作程序中使用Web浏览器对象

[英]How to use web browser object in background worker in C#

I have problem with code below. 我下面的代码有问题。 Can some one tell why it doesn't work in BackgroundWorker and how can i solve this problem 有人可以说出为什么它在BackgroundWorker中不起作用以及如何解决这个问题吗?

  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 您最好打开一个新线程并从那里指示WebBrowser

Application.DoEvents() is kinda evil . Application.DoEvents()有点邪恶

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: 要获得页面已加载的通知,您可以按以下方式订阅DocumentCompleted事件:

yourWebBrowser.DocumentCompleted += WebBrowserDocumentCompleted;

void WebBrowserDocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
    throw new NotImplementedException();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM