|
-
Apr 30th, 2006, 03:42 PM
#1
Thread Starter
New Member
[2005] multithreading - creating controls in a background thread
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:
'this is running in the background thread
Private Sub CreateResultsGrid()
Dim ResultsGrid As New DataGrid 'Create a new data grid
Dim Seperator As New Splitter 'and a new splitter
'code for intialising and populating grid goes below
'***
'***
'***
'prepare to marshal back to primary thread
Dim ObjArray(1) As Object
ObjArray(0) = ResultsGrid
ObjArray(1) = Seperator
ResultsPanel.Invoke(New Grid(AddressOf AddGrid), mySplitterGridArray)
End Sub
Public Delegate Sub Grid(ByVal myGrid As DataGrid, ByVal mySeperator As Splitter)
Public Sub AddPanelGrid(ByVal ResultsGrid As DataGrid, ByVal Seperator As Splitter)
'add the splitter and grid to the ResultsPanel
ResultsPanel.Controls.Add(Seperator) 'THIS WORKS
ResultsPanel.Controls.Add(ResultsGrid) 'THIS DOES NOT WORK: "Cross-thread operation not valid"
End Sub 'DelegateMethod
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|