C#System.Thread.ThreadStateException
我正在尝试以窗口形式初始化Web浏览器组件,但收到此异常消息C#System.Thread.ThreadStateException,c#,.net,webbrowser-control,C#,.net,Webbrowser Control,我正在尝试以窗口形式初始化Web浏览器组件,但收到此异常消息 System.Threading.ThreadStateException: 'ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.' 我在这条线上收到了上面提到的异常消息 this.browse =
System.Threading.ThreadStateException: 'ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.'
我在这条线上收到了上面提到的异常消息
this.browse = new System.Windows.Forms.WebBrowser();
请告诉我如何解决此问题。在main方法上方添加STAThread属性。用[STAThread]标记main()方法,如下所示:
[STAThread]
static void Main()
{
// your code
}
或者,如果应该从您创建的ant线程内部调用Web浏览器组件,请为此线程设置适当的单元状态:
yourThread.SetApartmentState(ApartmentState.STA);