Help on Adding tree node ??

G

Guest

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
 
D

Dave

The Control class requires methods to be invoked on the thread in which the Control was created. The event that is being handled
(your "OnChanged" event) is running on a thread-pooled thread.

Do something like this (sorry that my VB isn't very good):

Private Delegate AddNodeInvoker(Name as String)

Public Sub OnChanged(sender as Object, e As EventArgs)
If Me.tLogView.InvokeRequired Then
Dim _AddNode As New AddNodeInvoker(AddNode)
Dim Params As New Object(1)
Params(0) = e.Name
Me.tLogView.Invoke(_AddNode, Params)
Else
AddNode(e.Name)
End If
End Sub

Private Sub AddNode(Name as String)
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(Name, 2, 2)
m_Root.Nodes.Add(m_Node)
m_Root.Nodes.Add(m_Node)
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top