问题描述
我需要得到有关在HTML页面中应用CSS样式的信息。我用AxWebBrowser和迭代IHTMLDOMNode。我能够得到我所需要的数据,并移动code到我的应用程序。问题是,这部分是运行后台工作的内部,我试图实例控件时例外。
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.
有什么办法怎么解决这个或其他选项比AxWebBrowser?
Is there any way how to solve this or other option than AxWebBrowser?
推荐答案
您正在运行的问题是,大多数后台线程/工人的API将创建一个线程在多线程公寓状态。该错误信息表明控制要求线程是单线程单元。
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.
您可以解决此通过创建一个线程自己和指定的主题上的STA公寓状态。
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.STA);
t.Start();
这篇关于单线程公寓 - 不能实例化ActiveX控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!