【发布时间】:2014-07-23 21:37:55
【问题描述】:
首先是我的问题:
我尝试在后台工作人员中构建一个 winform。这个 Form 类只包含一个 webbrowser 控件。表单不会构建并跳出到主线程。 我在带有 WinForms 的 Visual Studio 2013 Pro 上使用带有 .NET 4.5 的 c#
我做什么:
启动后台工作者
private void bt_dashboard_chat_Click(object sender, EventArgs e)
{
if (!this.bw_webchat.IsBusy)
{
this.bw_webchat.RunWorkerAsync(this.auth.getUserName());
}
}
工人
{
String name = e.Argument as String;
DashBoard.Forms.Chat.DummyChat tmp = new Forms.Chat.DummyChat(name);
tmp.ShowDialog();
}
表单构造函数
InitializeComponent();
this.wb_twitchchat.Url = new Uri("http://link.tld/" + name + "/chat");
this.Text = "Chat of " + name;
问题。线程在 InitializeComponent() 处跳出;就行了
this.wb_twitchchat = new System.Windows.Forms.WebBrowser();
这是第二行。
有人知道为什么会这样吗?其他形式在后台工作线程中工作正常:/
【问题讨论】:
-
我的意思是在这一行之后 (this.wb_twitchchat = new System.Windows.Forms.WebBrowser();) 其余部分被跳过,没有发生任何事情,我又回到了主线程中。
-
那么它很可能会抛出异常。您是否检查了“输出”窗口中是否存在异常,可能没有中断执行?
-
忘记检查输出:/ Output:"System.Threading.ThreadStateException" in System.Windows.Forms.dll" 有什么想法吗?^^
-
我用 try catch 捕获了异常,我得到以下消息:“Das ActiveX-Steuerelement 8856f961-340a-11d0-a96b-00c04fd705a2 kann nicht instanziiert werden, da der aktuelle Thread kein Singlethread-Apartment ist 。”很抱歉德国异常消息,不知道如何更改它。
-
Ok 找到了解决方案:后台工作人员不支持 ApartmentState.STA 所以我需要使用 SetApartmentState(ApartmentState.STA); 切换到普通线程;现在它工作正常。问题是 Webbrowser 控件需要 ApartmentState.STA。
标签: c# winforms webbrowser-control .net-4.5 backgroundworker