Office BackgroundWorker and WebBrowser

Is it possible / recommended to use workflow threads using a web browser control?

I create a bot that searches google for keywords and then checks the sites on the first 10 pages to see if the site is ranked.

The user can provide a maximum of 20 sites to check and use proxies. Therefore, ideally, I would like to have 5 threads at once.

Is it possible? I may have heard that there are issues with WebBrowser control and threads.

+6
c #
source share
3 answers

This is not true. WebBrowser uses Internet Explorer, which is a COM component. COM components have a stream model, IE uses "Apartment". This is an expensive word that means that it is not thread safe. You can call its methods in BGW, but COM will automatically march the call into the UI thread. Since all method calls and property accesses actually occur in the user interface thread, you will do this more slowly with BGW.

In fact, you can run WebBrowser in another thread, you will need to create an instance of this thread. And you will need to create a stream, which is the so-called Single Threaded Apartment. STA, an abbreviation that you can easily recognize from the [STAThread] attribute in the Main () method of a Winforms or WPF application. To change the workflow to STA, you need to call Thread.SetApartmentState () before starting it. You cannot do this for BGW. And the thread must pump the message loop to implement the STA contract, it must call Application.Run (). For example, you need to force WebBrowser to raise its events. This answer shows the approach.

Consider using the WebRequest class.

+15
source share

Is there a reason you are using an IE control over a library like HTML Agility pack ? This supports multithreading without IE's COM nightmare and is much more powerful when parsing HTML.

+1
source share

To answer your immediate question: I never tried, but I would not be surprised if there were problems. Typically, WinForms controls are not designed to access threads other than the main UI thread. You must use the Control.Invoke() method to invoke calling methods from other threads. This queues them for the main user interface thread.

To solve a wider problem: you probably better not use the WebBrowser control at all unless you need to render HTML for the user to view. You can load the page using the HttpWebRequest class, which is much easier. WebBrowser is basically a full-blown Internet Explorer built into your application.

+1
source share

All Articles