Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#System.Thread.ThreadStateException_C#_.net_Webbrowser Control - Fatal编程技术网

C#System.Thread.ThreadStateException

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 =

我正在尝试以窗口形式初始化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 = new System.Windows.Forms.WebBrowser();

请告诉我如何解决此问题。

在main方法上方添加STAThread属性。

用[STAThread]标记main()方法,如下所示:

[STAThread]
static void Main()
{
    // your code
}
或者,如果应该从您创建的ant线程内部调用Web浏览器组件,请为此线程设置适当的单元状态:

yourThread.SetApartmentState(ApartmentState.STA);