首页 文章

C#WebBrowser错误线程

提问于
浏览
0

我无法在我的表单中使用Web浏览器 . 当我运行时,我得到此错误ActiveX控件'8856f961-340a-11d0-a96b-00c04fd705a2'无法实例化,因为当前线程不在单线程单元中 .

它带我到表单designer.cs文件到this text.webBrowser2 = new System.Windows.Forms.WebBrowser();而且我真的不知道如何让浏览器工作 .

我在Program.cs文件中尝试过MTAThread和STAThread似乎无法使其工作 .

谢谢

1 回答

  • 2

    您需要将线程标记为 STAThread ,因为COM控件需要公寓状态 .

    有两种简单的方法可以做到这一点:

    • 使用 [STAThread] 属性标记线程入口点(线程开头的函数) . 如果你没有申请't set it on the entry point but on some other method down the call stack, then this attribute won' .

    • 如果使用System.Threading.Thread类启动线程,则使用 Thread.SetApartmentState() 将线程的单元状态设置为 STAThread

相关问题