Hoping someone can help me with multi-threading, and creating/accessing controls in seperate threads.

Essentially, what I'm trying to do is create a number (indeterminate) of data grids in a background thread, populate them, and then somehow pass the grids back to the primary thread so that I can place them on my UI.

Because the number of grids I'm creating is unknown and also because populating them could take a while (due to large amounts) of data I cannot create the grids beforehand, which is why I'm trying to create them in the background thread.

Normally, when accessing controls created in a thread other than the one you're in I'd using the Invoke method but I'm not sure that's the case when trying to pass controls from one thread to the other. Plus, my attempt to do so doesn't work.

Here's a section of my code, with inline comments. If anyone can tell me what I might be doing wrong or how to best go about achieving the desired results that would be appreciated.

Note that I'm also creating a splitter control in my background thread and I successfully manage to create it on the UI.

VB Code:
  1. 'this is running in the background thread
  2.  
  3. Private Sub CreateResultsGrid()
  4.  
  5.     Dim ResultsGrid As New DataGrid    'Create a new data grid
  6.     Dim Seperator As New Splitter    'and a new splitter
  7.  
  8.     'code for intialising and populating grid goes below
  9.     '***
  10.     '***
  11.     '***
  12.    
  13.     'prepare to marshal back to primary thread
  14.      Dim ObjArray(1) As Object
  15.  
  16.      ObjArray(0) = ResultsGrid
  17.      ObjArray(1) = Seperator
  18.  
  19.      ResultsPanel.Invoke(New Grid(AddressOf AddGrid), mySplitterGridArray)
  20.  
  21. End Sub
  22.  
  23.  
  24. Public Delegate Sub Grid(ByVal myGrid As DataGrid, ByVal mySeperator As Splitter)
  25.  
  26. Public Sub AddPanelGrid(ByVal ResultsGrid As DataGrid, ByVal Seperator As Splitter)
  27.  
  28.     'add the splitter and grid to the ResultsPanel
  29.  
  30.     ResultsPanel.Controls.Add(Seperator)        'THIS WORKS  
  31.     ResultsPanel.Controls.Add(ResultsGrid)    'THIS DOES NOT WORK: "Cross-thread operation not valid"
  32.    
  33. End Sub 'DelegateMethod