C# WebBrowser Error Thread

独自空忆成欢 提交于 2019-12-25 02:18:56

问题


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 using Thread.SetApartmentState()


来源:https://stackoverflow.com/questions/7857112/c-sharp-webbrowser-error-thread

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!