Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Populating a tree view from another thread

Populating a tree view from another thread

Scheduled Pinned Locked Moved C#
databasecomdata-structuresquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Luis Alonso Ramos
    wrote on last edited by
    #1

    Hello, I've tried filling a tree view with a large database on a worker thread (so my main window keeps responding.) I get an InvalidOperationException saying that I'm doing it from the wrong thread. If I do it from the main thread, it is very slow, and can take up to 30 seconds. Any ideas? Thanks, -- LuisR ___________   Luis Alonso Ramos   Chihuahua, Mexico   www.luisalonsoramos.com

    C B 2 Replies Last reply
    0
    • L Luis Alonso Ramos

      Hello, I've tried filling a tree view with a large database on a worker thread (so my main window keeps responding.) I get an InvalidOperationException saying that I'm doing it from the wrong thread. If I do it from the main thread, it is very slow, and can take up to 30 seconds. Any ideas? Thanks, -- LuisR ___________   Luis Alonso Ramos   Chihuahua, Mexico   www.luisalonsoramos.com

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      DISCLAIMER: I've never tried this so I don't know if it will work. Would you be able to generate the tree structure first and then when your data loading thread is finished it fires an event to tell the main thread it is finished. The event handler then takes the nodes and attaches them to the tree control. This would mean that for 30 seconds you have an empty tree control - but at least the application is responsive.


      "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Not getting the response you want from a question asked in an online forum: How to Ask Questions the Smart Way!

      L 1 Reply Last reply
      0
      • C Colin Angus Mackay

        DISCLAIMER: I've never tried this so I don't know if it will work. Would you be able to generate the tree structure first and then when your data loading thread is finished it fires an event to tell the main thread it is finished. The event handler then takes the nodes and attaches them to the tree control. This would mean that for 30 seconds you have an empty tree control - but at least the application is responsive.


        "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Not getting the response you want from a question asked in an online forum: How to Ask Questions the Smart Way!

        L Offline
        L Offline
        Luis Alonso Ramos
        wrote on last edited by
        #3

        Mmm interesting idea... I'll give it a try! thanks! During thos 30 seconds, I could fill the tree with a "Populating tree control, please wait..." node. -- LuisR ___________   Luis Alonso Ramos   Chihuahua, Mexico   www.luisalonsoramos.com

        1 Reply Last reply
        0
        • L Luis Alonso Ramos

          Hello, I've tried filling a tree view with a large database on a worker thread (so my main window keeps responding.) I get an InvalidOperationException saying that I'm doing it from the wrong thread. If I do it from the main thread, it is very slow, and can take up to 30 seconds. Any ideas? Thanks, -- LuisR ___________   Luis Alonso Ramos   Chihuahua, Mexico   www.luisalonsoramos.com

          B Offline
          B Offline
          Baris Kurtlutepe
          wrote on last edited by
          #4

          If you want to modify a control from a thread other than the thread that created it, you should use the Invoke method of the Control (in that case the TreeView) to invoke the function that adds the node to the tree. A simple example for adding a node which takes a string parameter for the node could be: private void AddNode(string nodeText) { myTreeView.Nodes.Add(nodeText); } then you can declare a delegate to call this function: private delegate void AddNodeDelegate(string nodeText); and then from any thread, you can safely do this: string nodeText = "This is my safe node text"; AddNodeDelegate myAdd = new AddNodeDelegate(AddNode); myTreeView.Invoke(myAdd, new object[] { nodeText } );

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups