我想你應該移到新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,那麼你應該指出,它可能會導致明顯的隨機崩潰,這將很難調試。 –