473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 6483
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: alanrn | last post by:
I am using a TreeView to display the hierarchy of a strongly-typed collection (inherited from CollectionBase). The order of the nodes in the TreeView is strictly tied to the order in which they...
4
by: JoKur | last post by:
I'm currently using a context menu to allow the user to add, rename and delete nodes in a treeview control. When a node is added we give it a bogus name ("NewNode") and invoke the add method. We...
4
by: pmcguire | last post by:
I have a treeview with a lot of nodes. I want to load only the nodes that are initially visible when the form loads, and then continue to populate it in background and/or when the nodes are required...
0
by: stardust | last post by:
Hi everyone, Within a Winform application, a treeview is contained in a User Control and the User Control is then located within a splitview. When the UserControl added some nodes into the...
1
by: EOS | last post by:
Hi, I am new to PDA development and my background is purely C/C++. Now I can get the rought ideas on Compact C# .net for PDA development. But I am having troubles on adding TreeView with nodes...
3
by: juvi | last post by:
Hi, I have got a problem with Treeview.Nodes.Clear() under VB2005. When I have some nodes in my treeview and a force to clear() all nodes then it seems to work, because the nodes are not visible....
2
by: Kristopher Wragg | last post by:
I'm having some serious problems with the TreeView control. I've got a control that inherits TreeView and has some methods that firstly create a TreeNode then does some recursive procedure to add...
10
by: John Rogers | last post by:
This code only counts the parent nodes or rootnodes in a treeview, how do you count all the nodes in a treeview? // one way int NodeCounter = 0; foreach (TreeNode currentNode in...
6
Sl1ver
by: Sl1ver | last post by:
I've got a problem, i got the nodes to move but 1. they copy nodes(if you drag it to 3 different places it will actually have 3 of the same nodes) 2. i want to make the nodes, if moved update the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.