0

Getting cross thread error when executing tcViewer.TabPages.Add(t) statement.

Code is as below.

Private Function fff(t As TabPage)
    tcViewer.TabPages.Add(t)   'giving cross thread error
End Function

Function WebBrowserThread()
    Dim t As TabPage = New TabPage((k + 1).ToString())
    t.Name = k.ToString()
    tcViewer.Invoke(fff(t))
End Function

Please guide.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
dsi
  • 3,199
  • 12
  • 59
  • 102

2 Answers2

1

I think you should move the creation of the new TabPage onto the UI thread as well:

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

When you construct the TabPage, you eventually reach this call stack:

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) 

At this point, the Handle is being created, and if you're doing that on the wrong thread, everything else is going to start going wrong (because the thread that the control was created on isn't going to run a message pump)

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
-2

i don't know what invoking error you get but i suggest disabling cross-thread checking by adding this in the constructor or loaded event (very helpful when dealing with APIs)

Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False

Check this http://tech.xster.net/tips/invoke-ui-changes-across-threads-on-vb-net/

in wpf such problems are easy to fix because you have a single thread for all the controls (Dispatcher.Invoke)

Update

dealing with the UI controls have to be on the UI thread

Me.Invoke(sub()
            Dim t As TabPage = New TabPage((k + 1).ToString())
            t.Name = k.ToString()
            fff(t)
          End Sub)




  Me.Invoke(sub()
               tcViewer.TabPages.Add(t)
            End Sub)
Ahmed Fwela
  • 955
  • 1
  • 13
  • 32
  • In WIN Form, set cross thread checking false , but getting same error as below `Controls created on one thread cannot be parented to a control on a different thread.` Do you know how to use `Invoke` of tab control to add tab page ? – dsi Jun 25 '15 at 09:12
  • try this > Me.Invoke(Sub() Dim t As TabPage = New TabPage((k + 1).ToString()) t.Name = k.ToString() fff(t) End Sub) – Ahmed Fwela Jun 25 '15 at 09:19
  • @bigworld12 If you're going to suggest using CheckForIllegalCrossThreadCalls = False then you should also point out that it may lead to apparently random crashes which will be difficult to debug. – Andrew Morton Jun 25 '15 at 09:30
  • 2
    [Is it good idea to use “Control.CheckForIllegalCrossThreadCalls = false”](http://stackoverflow.com/q/15516014/15498). The answer is no. – Damien_The_Unbeliever Jun 25 '15 at 09:31
  • i have been using it for like 4 years until i moved to WPF , not a single crash or bug – Ahmed Fwela Jun 25 '15 at 09:33
  • 2
    They have a threading bug. Your suggestion is "turn off the feature that made it obvious that you have a threading bug". – Damien_The_Unbeliever Jun 25 '15 at 09:41
  • i see it like this : why add 2 more lines of code for a thing that can be done in one line ? – Ahmed Fwela Jun 25 '15 at 09:43
  • 1
    And putting black tape over the engine warning light is quicker and easier than taking the car to the garage. It doesn't make it the right thing to do. – Damien_The_Unbeliever Jun 25 '15 at 10:11