Results 1 to 5 of 5

Thread: [2005] multithreading - creating controls in a background thread

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    7

    [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:
    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

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    7

    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

    Quote 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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    7

    Re: [2005] multithreading - creating controls in a background thread

    Quote 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
  •  



Click Here to Expand Forum to Full Width