C# WebBrowser 错误线程
我无法让我的表单中的网络浏览器正常工作。当我运行时,我收到此错误 ActiveX 控件“8856f961-340a-11d0-a96b-00c04fd705a2”无法实例化,因为当前线程不在单线程单元中。
它带我到表单 Designer.cs 文件中的此文本 this.webBrowser2 = new System.Windows.Forms.WebBrowser();我真的不知道该怎么做才能让浏览器正常工作。
我在 Program.cs 文件中尝试了 MTAThread 和 STAThread 似乎无法让它工作。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将线程标记为 STAThread,因为 COM 控件需要该单元状态。
有两种简单的方法可以做到这一点:
[STAThread]
属性标记线程入口点(线程开始的函数)。如果您没有在入口点上设置它,而是在调用堆栈中的其他方法上设置它,则不会应用此属性。You need to mark your thread as an
STAThread
, because COM controls require that apartment state.There are two easy ways to do this:
[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.STAThread
usingThread.SetApartmentState()