Adding nodes dynamically ???


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Adding nodes dynamically ???

Hybrid View

  1. #1
    Join Date
    Jan 2004
    Location
    France and work in switzerland
    Posts
    105

    Adding nodes dynamically ???

    Dear all,

    I have an application which timely create log file in XML format
    I ma using the SystemFileWatcher object to monitor creation of new file in the proper path. So far no problem

    When the first file is created, my SystemFile onchanged event is catch and then I add the new log file in a treview control with the following code:

    Dim m_Root As New TreeNode
    m_Root = Me.tLogView.Nodes.Item(0)
    'For idx = 0 To sFile.Count - 1
    Dim m_Node As New TreeNode(e.Name, 2, 2)
    m_Root.Nodes.Add(m_Node)
    m_Root.Nodes.Add(m_Node)

    When the second file is created, event is cacthed again, and I am suppose to add the new file to the treeview as previous step. But at that time I have an exception which says something like I am not in the current thread and I need to call Control.Invoke before adding my new node ???

    What sors it means ? how can add new nodes when Onchange event occurs?

    thaks for your help
    regards
    serge
    Thanks for your help
    Regards

    CALDERARA Serge
    Maillefer S.A

  2. #2
    Join Date
    Nov 2004
    Location
    Huddinge, Sweden
    Posts
    283
    What tis means is that every window and its controls is owned by a thread. Whenever you manipulate the window from the "wrong" thread there's a chance things might not work well, and sometimes an exception is thrown.
    Control.Invoke and Control.BeginInvoke are methods that transfer execution to the owning thread.

    What you need to do (and I know this is a bit cruddy) is something like this:
    Code:
        private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
        {
          if (this.InvokeRequired)
          {
            this.BeginInvoke(new System.IO.FileSystemEventHandler(fileSystemWatcher1_Changed), new object[] {e});
          }
          else
          {
            MyTree.Nodes.Add(e.ToString());
          }
        }
    This will result in the methood calling itself on the correct thread if needed.

    Using BeginInvoke(), rather than Invoke() makes the call asyncronous. Most of the time it doesn't matter, but when you're not interested in the result anyway it can sometimes create a more responsive app.

    Rune

    PS. Ignore the spurious space that the forum inserts in the middle of the BeginInvoke call...
    If you hit a brick wall, you didn't jump high enough!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center