471,319 Members | 3,066 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

How to add nodes into TreeView via Threads?

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?
Nov 29 '05 #1
12 6383
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.

"EOS" <no****@nospam.org> wrote in message
news:ew**************@TK2MSFTNGP12.phx.gbl...
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?

Nov 29 '05 #2
EOS
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...

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:uI**************@TK2MSFTNGP15.phx.gbl...
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.

"EOS" <no****@nospam.org> wrote in message
news:ew**************@TK2MSFTNGP12.phx.gbl...
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?


Nov 29 '05 #3
EOS wrote:
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/t...winforms.shtml

Jon

Nov 29 '05 #4
EOS
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... : )

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
EOS wrote:
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/t...winforms.shtml

Jon

Dec 1 '05 #5
EOS
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... : (

"EOS" <no****@nospam.org> wrote in message
news:en*************@TK2MSFTNGP12.phx.gbl...
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... : )

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
EOS wrote:
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/t...winforms.shtml

Jon


Dec 1 '05 #6
> 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

Dec 1 '05 #7
EOS
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. : )
Dec 1 '05 #8
EOS <no****@nospam.org> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 1 '05 #9
EOS
Sound like my headache is BIGGER that I initially tought... : (

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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
EOS <no****@nospam.org> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 1 '05 #10
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

Dec 2 '05 #11
EOS
Thanks for the solution. : )

"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Dec 2 '05 #12
jmR
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.

Jan 13 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by alanrn | last post: by
4 posts views Thread by JoKur | last post: by
2 posts views Thread by Kristopher Wragg | last post: by
10 posts views Thread by John Rogers | last post: by
Sl1ver
6 posts views Thread by Sl1ver | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.