问题
I am having trouble getting a web browser in my form to work. When I run, I get this error ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
It takes me to the form designer.cs file to this text this.webBrowser2 = new System.Windows.Forms.WebBrowser(); and I really do not know what to do to get thebrowser working.
I have tried both MTAThread and STAThread in the Program.cs file cant seem to get it to work.
Thanks
回答1:
You need to mark your thread as an STAThread
, because COM controls require that apartment state.
There are two easy ways to do this:
- Mark your thread entry point (the function that your thread begins with) with the
[STAThread]
attribute. If you don't set it on the entry point but on some other method down the call stack, then this attribute won't be applied. - If you are starting the thread using the System.Threading.Thread class, then set the apartment state of the thread to an
STAThread
usingThread.SetApartmentState()
来源:https://stackoverflow.com/questions/7857112/c-sharp-webbrowser-error-thread