|
-
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
-
Apr 30th, 2006, 03:55 PM
#2
Re: [2005] multithreading - creating controls in a background thread
Don't use controls to store data. That is a crime of the highest order. Store it in RAM and/or cached disc files and populate a single control as and when required. You will ruin the performance of your program if you overload the GUI. Sounds like a re-design is in order. Plus multithreading is a nightmare when the GUI is involved anyway.
I don't live here any more.
-
Apr 30th, 2006, 05:34 PM
#3
Thread Starter
New Member
Re: [2005] multithreading - creating controls in a background thread
Thanks for the reply. I was worried that this was going to be the response. I can live with re-designing, if I only knew what to design to.
I can't really get away from the dynamic data grid model that I have. For example, I won't know how many columns or the properties of the datagrid until runtime. The grid itself could contain (theoretically) hundreds of thousands or rows. As such I didn't want the user waiting around for the grid to fill up so I thought I'd run it in a background thread.
For some reason I thought it would be really great if I could create (and populate) the grid in the background, and when done just pop it onto the UI. Well, the idea seemed good.
As you pointed out, and I'm finding out, multithreading is a nightmare when the GUI is involved
Karl
 Originally Posted by wossname
Don't use controls to store data. That is a crime of the highest order. Store it in RAM and/or cached disc files and populate a single control as and when required. You will ruin the performance of your program if you overload the GUI. Sounds like a re-design is in order. Plus multithreading is a nightmare when the GUI is involved anyway.
-
Apr 30th, 2006, 06:25 PM
#4
Re: [2005] multithreading - creating controls in a background thread
If you're using 2005 then I strongly recommend using the DataGridView over the DataGrid. Then you can set VirtualMode to True and populate the grid with data as needed.
-
Apr 30th, 2006, 06:28 PM
#5
Thread Starter
New Member
Re: [2005] multithreading - creating controls in a background thread
 Originally Posted by jmcilhinney
If you're using 2005 then I strongly recommend using the DataGridView over the DataGrid. Then you can set VirtualMode to True and populate the grid with data as needed.
Thanks for the tip. I'll look into it. I literally just started with 2005 this weekend and I'm still not aware of all the new stuff.
I noticed that multi-threading is a lot tighter with 2005 than 2003, which is a good thing really.
Karl
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
|