views:

790

answers:

3

Is it possible to create a WebBrowser control in a background thread in BackgroundWorker?

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Using web1 As New WebBrowser

    End Using
End Sub

This throws the following error:

ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.

Does anyone know a way to create a background WebBrowser? I would like to do it to extract info from websites then spit out the various HTML DOM arrays

A: 

The thread that the Webbrowser runs in must be ApartmentState.STA. From a BackgroundWorker thread you'll have to use Invoke on a UI object and add the Webbrowser in that context.

I don't think you really need to run the WebBrowser object in a background thread anyway. All of it's functionality is handled asynchronously with events.

Bob Nadler
im not trying to add a webbrowser to the form, but load it as a class and use it's dom functions to extract info from a website, end goal is to ferry the htmldocument object to the backgroundworker result object
Jim
OK. Then instead of using BackgroundWorker, create a real thread and use the .SetApartmentState(ApartmentState.STA). That may work.
Bob Nadler
A: 

You can also write a wrapper class, that instantiates the web browser control, create methods that wrap your extraction functions, and fire off the results through public events.

From here you just create a new instance of your wrapper, and add it to the ThreadPool.

Remember to do proper owner-thread invoking to avoid cross-thread exceptions ;)

Wez
A: 

Having been down both roads, I would recommend WatiN, an open source library to automate IE and Firefox. The WebBrowser control is good for displaying content, but for anything more complex it becomes a big hassle.

Adam Neal
hmm not trying to display content only extract the dom elements with the jscript client side manipulation intact, for instance if the jscript alters a image on page load, i want the resulting HTML Document and dom structure which that i can manipulate in program, thinking i could escape using the webbrowser. i wasnt expecting to be slowed down by the threading issue
Jim
right now stuck on how do i make threads do something useful, as i dont understand the THREAD constructor right now, too used to backgroundworkers
Jim