How to add nodes into TreeView via Threads?

  • Thread starter Thread starter EOS
  • Start date Start date
E

EOS

Hi,

I am dynamically created TreeView and able to update/add nodes using button.
The problem is nothing is being done if I was using threads.

Any ideas and suggestion?
 
Remember that you should not access data on the UI thread directly. You'll
need to create an AddNode method in the UI thread and then invoke that from
your thread.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
I actually use similar method of PictureBox and it works...

Sound like the only way is the AddNote in UI? But problem I only receive the
data on threads, then I know what to be added into the TreeView.

Hope my explanaition is clear enought...
 
EOS said:
I actually use similar method of PictureBox and it works...

That doesn't mean it should work, that it will always work, or even
that it currently works in all situations.
Sound like the only way is the AddNote in UI? But problem I only receive the
data on threads, then I know what to be added into the TreeView.

Hope my explanaition is clear enought...

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml

Jon
 
It is kind of complicated after reading that. In fact, I still yet to have
any working ideas.

Does anyone has an easy way just to add (no need delete) nodes and items
into TreeView via threads?

Hope there is a easy way for a simple question... : )
 
I hope some straight forward sample will be best...

In C/C++, I can do a lot of wonders with pointers. While in the world of C#,
I found it is not really C/C++ after all... : (
 
In fact, I still yet to have
any working ideas.

Try something like this:

public delegate TreeViewAddRemoveDelegate(TreeNode parentNode, TreeNode
nodeToAdd);

public void AddNode(TreeView tree, TreeNode parentNode, TreeNode
nodeToAdd)
{
if (!treeView.InvokeRequried)
{
parentNode.Nodes.Add(nodeToAdd);
}
else
{
tree.Invoke(new TreeViewAddRemoveDelegate(AddNode), new object[]
{parentNode, childNode} );
}
}

Then you can call AddNode from any thread. If wanna remove, write a
similar RemoveNode method.

Thi
 
Hi,

I am sorry to supply the information that it is meant for compact .net, PDA.

Hope there is a solution for such PDA. And hopefull an easy and straight
forward one. : )
 
EOS said:
I am sorry to supply the information that it is meant for compact .net, PDA.

Hope there is a solution for such PDA. And hopefull an easy and straight
forward one. : )

Unfortunately, things are slightly less simple on the Compact
Framework, as (IIRC) only EventHandler delegates can be invoked using
Control.Invoke.

I suggest you ask on the CF group for more information - but the long
and the short of it is that you'll need to make the changes on the UI
thread.
 
Sound like my headache is BIGGER that I initially tought... : (

Hope someone can shine some lights to confussing me...
 
Hi,

..NET framework does not support InvokeRequired and does not allow you
to pass arguments to the delegate called by Invoke. A workaround might
be using a private to store the node to add. It might be OK as long as
updating UI is done less than often from the non-UI thread. That worker
thread should focus on its main task, and only update UI on some
occasions.

// these var is used to communicate between threads
private TreeNode parentNode;
private TreeNode nodeToAdd;
private object syncObj = new Object();

public void AddNodeFromUIThread(object sender, System.EventArgs e)
{
if (parentNode != null)
{
parentNode.Nodes.Add(nodeToAdd);
}
else
{
TreeView t = (TreeView) sender;
t.Nodes.Add(nodeToAdd);
}
}

public void AddNodeFromOtherThread(TreeNode parentNode, TreeNode
nodeToAdd)
{
lock (syncObj)
{
this.parentNode = parentNode;
this.nodeToAdd = nodeToAdd;

treeView1.Invoke(new EventHandler(AddNodeFromUIThread));
}
}

Thi
 
Thank you for your help. I was looking in my code why I got an argument
exception when I was trying to use Invoke on the compact framework without
success... The signature of my delegate was not EventHandler like.

This is not explain on the MSDN invoke documentation.
 
Back
Top