C#的WebBrowser错误线程 [英] C# WebBrowser Error Thread

查看:170
本文介绍了C#的WebBrowser错误线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦在我的形式工作的Web浏览器。当我跑,我得到这个错误的ActiveX控件8856f961-340a-11D0-A96B-00c04fd705a2'不能被实例化,因为当前线程不在单线程单元。

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.

这要花我到表单designer.cs文件,该文本this.webBrowser2 =新System.Windows.Forms.WebBrowser();我真的不知道该怎么做才能得到thebrowser工作。

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.

我在Program.cs的尝试都MTAThread和STAThread文件不能似乎得到它的工作。

I have tried both MTAThread and STAThread in the Program.cs file cant seem to get it to work.

感谢

推荐答案

您需要标记您的线程为 STAThread ,因为COM控件需要在公寓的状态。

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] 属性。如果你不把它放在入口点,但是,一些其他的方法下调用堆栈,那么这个属性将不会被应用。

  • 如果你开始使用System.Threading.Thread类线程,然后设置线程的单元状态到 STAThread 使用发.SetApartmentState()

  • 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()

这篇关于C#的WebBrowser错误线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆