strange problem adding child node to a parent node

G

Guest

Ok, so I'm absoultely positive this was working last night, but today I'm
getting this error in my program when I try to add a child node to a parent
node of a tree...

The error says:

Message: The action being performed on this control is being called from the
wrong thread. You must marshal to the correct thread using Control.Invoke or
Control.BeginInvoke to perform this action.
Stack Trace: at System.Windows.Forms.TreeNode.Realize()
at System.Windows.Forms.TreeNodeCollection.Add(TreeNode node)
at test.MyNode.AddChild(MyBaseNode node) in ...

So what does this all mean? By the time this error occurs it has already
gone through this same method adding about 30 nodes (confirmed by output)....
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi,

The error message says it all - you are most likely accessing the TreeView
control from a worker thread, which is prohibited.
 
G

Guest

Thanks for your reply! Well, I'm not exactly accessing a Treeview from a
worker thread, what I'm doing is having a worker thread create a root
TreeNode and then add children to it, then later when everything is done and
I'm ready to display this to the UI , I create the dialog, the TreeView and
just add this root node.

Do I have to take the extreme step of creating a non-tree-specific structure
in this worker thread, then once I'm ready for the UI to convert it into a
tree?
 
D

Dmitriy Lapshin [C# / .NET MVP]

I think you can create the hierarchy of TreeNodes on a worker thread, but
once the hierarchy is ready, the rest should happen on the UI thread.
In particular, make sure you assign the root node to the treeview on the UI
thread.

If you still get the exception, you will indeed have to create a parallel
hierarchical structure on the worker thread and to convert it to tree nodes
on the UI thread.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx
 

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