views:

3859

answers:

2

I need to get information about applied CSS styles in HTML page. I used AxWebBrowser and iterate IHTMLDOMNode. I'm able to get all the data I need and move the code into my application. The problem is that this part is running inside of the background worker and I got exception when trying to instantiate the control.

AxWebBrowser browser = new AxWebBrowser();

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

Is there any way how to solve this or other option than AxWebBrowser?

+4  A: 

The problem you're running into is that most background thread / worker APIs will create the thread in a Multithreaded Apartment state. The error message indicates that the control requires the thread be a Single Threaded Apartment.

You can work around this by creating a thread yourself and specifying the STA apartment state on the thread.

var t = new Thread(MyThreadStartMethod);
t.SetApartmentState(ApartmentState.MTA);
t.Start();
JaredPar
Thanks, great this is working. One more questions/problem. The class I'm using is just class and the AxWebBrowser looks like it needs to be added into this.Controls(). Is there way how to fake the Controls? Or will I need to have separated Form for that?
martin.malek
@martin.malek There's no great way to fake that. The best bet is to create a new form.
JaredPar
A: 

Thanks alot...It helped me.....

rakhy_rakey