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
  1. Home
  2. General Programming
  3. C#
  4. Populating TreeViews with data acquired from void static callback methods

Populating TreeViews with data acquired from void static callback methods

Scheduled Pinned Locked Moved C#
question
2 Posts 2 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.
  • I Offline
    I Offline
    inyoursadachine
    wrote on last edited by
    #1

    Hi, I'm trying to populate a TreeView with info I'm gathering via a PeekCompleted callback for a MessageQueue. My PeekCompleted method, which is "void static", does not have any trouble populating a ListBox but the compiler complains that I must use Control.Invoke() from the callback to use TreeNode.Add(). Here are my questions: 1) Why? 2) Should I use Control.Invoke() or do this another way? All I'm trying to do is build a TreeView using data I collected from a callback method. What are my other options? TIA, Matt

    H 1 Reply Last reply
    0
    • I inyoursadachine

      Hi, I'm trying to populate a TreeView with info I'm gathering via a PeekCompleted callback for a MessageQueue. My PeekCompleted method, which is "void static", does not have any trouble populating a ListBox but the compiler complains that I must use Control.Invoke() from the callback to use TreeNode.Add(). Here are my questions: 1) Why? 2) Should I use Control.Invoke() or do this another way? All I'm trying to do is build a TreeView using data I collected from a callback method. What are my other options? TIA, Matt

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Yes you should use Control.Invoke. This invokes the call on the thread on which the control was created, which is important. You should do this for your ListView as well. Modifying the control from a different thread causes problems in the message queue. The technical details get down into what is encapsulated by the .NET Framework (much of it, anyway): Win32 APIs. To use Invoke, take a look at this sample code to safely add a TreeNode in the property thread:

      internal void SafeAdd(TreeNodeCollection nodes, TreeNode node)
      {
      if (nodes == null || node == null)
      throw new ArgumentNullException(nodes == null ? "nodes" : "node");
       
      if (treeView1.InvokeRequired)
      {
      Delegate d = new AddTreeNodeHandler(nodes.Add);
      treeView1.Invoke(d, new object[] {node});
      }
      else nodes.Add(node);
      }
      private delegate int AddTreeNodeHandler(TreeNode node);

      Microsoft MVP, Visual C# My Articles

      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