我想你应该移到新TabPage
的创建到UI线程以及上:
Private Function fff(k as Integer)
Dim t As TabPage = New TabPage((k + 1).ToString())
t.Name = k.ToString()
tcViewer.TabPages.Add(t)
End Function
Function WebBrowserThread()
tcViewer.Invoke(fff(k))
End Function
当你构建TabPage
,你最终达到这个调用堆栈:
System.Windows.Forms.dll!System.Windows.Forms.Control.CreateHandle()
System.Windows.Forms.dll!System.Windows.Forms.Application.MarshalingControl.MarshalingControl()
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.MarshalingControl.get()
System.Windows.Forms.dll!System.Windows.Forms.WindowsFormsSynchronizationContext.WindowsFormsSynchronizationContext()
System.Windows.Forms.dll!System.Windows.Forms.WindowsFormsSynchronizationContext.InstallIfNeeded()
System.Windows.Forms.dll!System.Windows.Forms.Control.Control(bool autoInstallSyncContext)
System.Windows.Forms.dll!System.Windows.Forms.ScrollableControl.ScrollableControl()
System.Windows.Forms.dll!System.Windows.Forms.Panel.Panel()
System.Windows.Forms.dll!System.Windows.Forms.TabPage.TabPage()
System.Windows.Forms.dll!System.Windows.Forms.TabPage.TabPage(string text)
此时,正在创建Handle
,一d如果你是在错误的线程上执行该操作,则其他一切都将开始出错(因为创建控件的线程不会运行消息泵)
在WIN窗体中,设置交叉线程检查错误,但得到相同的错误,如下所示:'在一个线程上创建的控件不能用于不同线程上的控件。'你知道如何使用tab控件的Invoke来添加标签页吗? – dsi
尝试此> Me.Invoke(子() 昏暗T作为TabPage的=新的TabPage((K + 1)的ToString()) t.Name = k.ToString() FFF(吨) 完子) – bigworld12
@ bigworld12如果你打算建议使用CheckForIllegalCrossThreadCalls = False,那么你应该指出,它可能会导致明显的随机崩溃,这将很难调试。 –