473,387 Members | 1,664 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,387 software developers and data experts.

InvokeRequired

Hi there,

I need to iterate all "TreeView" nodes on a worker thread. When doing so,
I'll call "InvokeRequired" and route all method/property calls through
"Invoke()" as required. However, for each "TreeNode" object I encounter in
the tree, must I also route method/property calls on this object through
"Invoke()" as well. I wouldn't think so since a "TreeNode" is not a control
in its own right so its methods/properties would presumably take care
calling "Invoke()" themselves if necessary (if they need to call the
"TreeView" methods for something). Is this correct? Thanks very much.
Dec 19 '06 #1
15 3931
Samuel White wrote:
Hi there,

I need to iterate all "TreeView" nodes on a worker thread. When doing so,
I'll call "InvokeRequired" and route all method/property calls through
"Invoke()" as required. However, for each "TreeNode" object I encounter in
the tree, must I also route method/property calls on this object through
"Invoke()" as well. I wouldn't think so since a "TreeNode" is not a control
in its own right so its methods/properties would presumably take care
calling "Invoke()" themselves if necessary (if they need to call the
"TreeView" methods for something). Is this correct? Thanks very much.

I think you are correct. All the controls are running in the same UI
thread, so if you did the check on the TreeView control, there is no
need to call it again on the TreeNode again..

HTH.
J.W.
Dec 19 '06 #2
Samuel,

You'll need to limit access to a TreeNode as you would any control.
That rule applies to almost everything in the System.Windows.Form
namespace.

Brian

Samuel White wrote:
Hi there,

I need to iterate all "TreeView" nodes on a worker thread. When doing so,
I'll call "InvokeRequired" and route all method/property calls through
"Invoke()" as required. However, for each "TreeNode" object I encounter in
the tree, must I also route method/property calls on this object through
"Invoke()" as well. I wouldn't think so since a "TreeNode" is not a control
in its own right so its methods/properties would presumably take care
calling "Invoke()" themselves if necessary (if they need to call the
"TreeView" methods for something). Is this correct? Thanks very much.
Dec 19 '06 #3
Brian Gideon wrote:
Samuel,

You'll need to limit access to a TreeNode as you would any control.
That rule applies to almost everything in the System.Windows.Form
namespace.

Brian

Samuel White wrote:
>Hi there,

I need to iterate all "TreeView" nodes on a worker thread. When doing so,
I'll call "InvokeRequired" and route all method/property calls through
"Invoke()" as required. However, for each "TreeNode" object I encounter in
the tree, must I also route method/property calls on this object through
"Invoke()" as well. I wouldn't think so since a "TreeNode" is not a control
in its own right so its methods/properties would presumably take care
calling "Invoke()" themselves if necessary (if they need to call the
"TreeView" methods for something). Is this correct? Thanks very much.
But if he already checked the TreeView Control, does he need to check
each TreeNode again. I assume after he checked TreeView control, the
thread should already be marshaled properly to the UI thread.
Dec 19 '06 #4
But if he already checked the TreeView Control, does he need to check each
TreeNode again. I assume after he checked TreeView control, the thread
should already be marshaled properly to the UI thread.
Thanks for both your feedback. Keep in mind however that a "TreeNode" isn't
a control itself. My thinking is that I simply need to call "TreeView.Nodes"
via "Invoke()" which then returns a "TreeNodeCollection" (a collection of
"TreeNode" objects). Once I have this collection however, shouldn't I be
free to call into it directly since it's not a control itself? If it needs
to talk to the "TreeView" for some reason then shouldn't that be its own
responsibility? BTW, it does work problem-free this way but it's still no
guarantee I'm doing it correctly (and could break at some later point).
Dec 19 '06 #5
Samuel White wrote:
>But if he already checked the TreeView Control, does he need to check each
TreeNode again. I assume after he checked TreeView control, the thread
should already be marshaled properly to the UI thread.

Thanks for both your feedback. Keep in mind however that a "TreeNode" isn't
a control itself. My thinking is that I simply need to call "TreeView.Nodes"
via "Invoke()" which then returns a "TreeNodeCollection" (a collection of
"TreeNode" objects). Once I have this collection however, shouldn't I be
free to call into it directly since it's not a control itself? If it needs
to talk to the "TreeView" for some reason then shouldn't that be its own
responsibility? BTW, it does work problem-free this way but it's still no
guarantee I'm doing it correctly (and could break at some later point).

I believe you are doing correctly as long as you make sure you check
InvokeRquired when you call the TreeView control.
Dec 19 '06 #6

Jianwei Sun wrote:
Brian Gideon wrote:
Samuel,

You'll need to limit access to a TreeNode as you would any control.
That rule applies to almost everything in the System.Windows.Form
namespace.

Brian

But if he already checked the TreeView Control, does he need to check
each TreeNode again. I assume after he checked TreeView control, the
thread should already be marshaled properly to the UI thread.
No that's fine. The bottom line is that a TreeNode can only be
accessed from the UI thread and shouldn't be seen as an exception to
the rule just because it doesn't subclass Control.

Dec 19 '06 #7
"Samuel White" <no_spam@_nospam.comwrote:
>I need to iterate all "TreeView" nodes on a worker thread.
(in addition to the other more useful replies in this thread...)

Do you really need to do this? It sounds like you might be storing
your data in UI objects, when they'd be better of being stored in your
own UI-independent objects that you make yourself. That way the worker
threads won't need to interact with the UI.

If you're trying to get two threads to interact with the UI to gain
performance, it won't really gain performance. It'd probably cause
slowdown due to all the synchronization.

--
Lucian
Dec 19 '06 #8

Samuel White wrote:
Thanks for both your feedback. Keep in mind however that a "TreeNode" isn't
a control itself. My thinking is that I simply need to call "TreeView.Nodes"
via "Invoke()" which then returns a "TreeNodeCollection" (a collection of
"TreeNode" objects).
Anything relating to a TreeView object can only be accessed from the UI
thread. That includes the TreeNodeCollection even though it doesn't
subclass Control.
Once I have this collection however, shouldn't I be
free to call into it directly since it's not a control itself?
No. You cannot touch it from a worker thread because it may be bound
to the TreeView.
If it needs
to talk to the "TreeView" for some reason then shouldn't that be its own
responsibility?
No. It simply won't do that.
BTW, it does work problem-free this way but it's still no
guarantee I'm doing it correctly (and could break at some later point).
Like you said, it may appear to work, but that doesn't mean it's
correct.

Dec 19 '06 #9
No that's fine. The bottom line is that a TreeNode can only be
accessed from the UI thread and shouldn't be seen as an exception to
the rule just because it doesn't subclass Control.
Thanks but can you clarify. The pattern I'm hoping to use is this:

1) On the worker thread, use "Invoke()" to call "TreeView.Nodes" which
returns the "TreeNode" collection
2) Access the collection returned in 1) above directly from the worker
thread itself (no need to call "Invoke" again since the collection isn't a
control)

Will this work or is item 2 incorrect (i.e., does "Invoke" still need to be
called and if so why?)
Dec 19 '06 #10
"Samuel White" <no_spam@_nospam.comwrote:
>>I need to iterate all "TreeView" nodes on a worker thread.

(in addition to the other more useful replies in this thread...)

Do you really need to do this? It sounds like you might be storing
your data in UI objects, when they'd be better of being stored in your
own UI-independent objects that you make yourself. That way the worker
threads won't need to interact with the UI.

If you're trying to get two threads to interact with the UI to gain
performance, it won't really gain performance. It'd probably cause
slowdown due to all the synchronization.
The worker thread is kicked off via a button handler on a dialog. I need to
iterate a "TreeView" on the dialog from this thread. I considered copying
the tree nodes to another (binary-tree) collection first but don't see any
tree-based collection available (unless you can recommend one - it
effectively has to provide the same parent/child relationships as a
"TreeView"). I don't want to bother writing my own for this purpose. If I
have to repeatedly call into the nodes collection on the tree's thread I
will but I wanted to avoid this obviously. I think any degradation in
performance by doing so will be minimal however since the time required to
iterate a few hundred or even a few thousand nodes should be negligible
(especially when compared to the other processing required on my worker
thread). Thanks for your help.
Dec 19 '06 #11
Samuel White wrote:
>No that's fine. The bottom line is that a TreeNode can only be
accessed from the UI thread and shouldn't be seen as an exception to
the rule just because it doesn't subclass Control.

Thanks but can you clarify. The pattern I'm hoping to use is this:

1) On the worker thread, use "Invoke()" to call "TreeView.Nodes" which
returns the "TreeNode" collection
2) Access the collection returned in 1) above directly from the worker
thread itself (no need to call "Invoke" again since the collection isn't a
control)

Will this work or is item 2 incorrect (i.e., does "Invoke" still need to be
called and if so why?)

If you want to call the TreeNode collection which is on the UI thread
from the worker thread, you still need to call "Invoke" to marshal the
call to the UI thread again.

Dec 19 '06 #12

Samuel White wrote:
1) On the worker thread, use "Invoke()" to call "TreeView.Nodes" which
returns the "TreeNode" collection
2) Access the collection returned in 1) above directly from the worker
thread itself (no need to call "Invoke" again since the collection isn't a
control)

Will this work or is item 2 incorrect (i.e., does "Invoke" still need to be
called and if so why?)
Item 2 is incorrect. Invoke still needs to be called. The reason is
because the collection is obtained from the control and could be bound
to it. In fact, in the case of TreeView and TreeNodeCollection that's
precisely the setup. Calling members on TreeNodeCollection in turn
calls members on TreeView. The same true for TreeNode. This can be
verified by looking at the IL code.

It may be working for you, but that's either by accident or
coincidence. If you continue to do it then it may eventually fail.

Dec 19 '06 #13
>1) On the worker thread, use "Invoke()" to call "TreeView.Nodes" which
>returns the "TreeNode" collection
2) Access the collection returned in 1) above directly from the worker
thread itself (no need to call "Invoke" again since the collection isn't
a
control)

Will this work or is item 2 incorrect (i.e., does "Invoke" still need to
be
called and if so why?)

Item 2 is incorrect. Invoke still needs to be called. The reason is
because the collection is obtained from the control and could be bound
to it. In fact, in the case of TreeView and TreeNodeCollection that's
precisely the setup. Calling members on TreeNodeCollection in turn
calls members on TreeView. The same true for TreeNode. This can be
verified by looking at the IL code.

It may be working for you, but that's either by accident or
coincidence. If you continue to do it then it may eventually fail.
Ok, thanks for yours and everyone else's help. BTW, do you know of any
tree-based collection class I can use instead. I'll copy my "TreeView" into
it on the main GUI thread and hand it to my worker thread. Thanks again.
Dec 19 '06 #14

Samuel White wrote:
Ok, thanks for yours and everyone else's help. BTW, do you know of any
tree-based collection class I can use instead. I'll copy my "TreeView" into
it on the main GUI thread and hand it to my worker thread. Thanks again.
I'm not aware of a collection that would be suitable. You'll probably
have to write your own. But, you're on the right track now.

Dec 19 '06 #15
I'm not aware of a collection that would be suitable. You'll probably
have to write your own. But, you're on the right track now.
Yes, thanks to you and the others (appreciated).
Dec 19 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sgt. Sausage | last post by:
Problem. Work-around. No issues, just looking to see if I'm the only one that's seen this. I just lost 3 hours (shoulda found it sooner) to the InvokeRequired on a form being somewhat...
2
by: BoloBaby | last post by:
Earlier, I had a threading issue where I had to use the InvokeRequired to get my controls to function properly. Does InvokeRequired apply to my custom classes as well? That is, if I have...
2
by: Samuel R. Neff | last post by:
I'm trying to find a good way to handle Control.InvokeRequired without duplicating four lines of code in every function/event. Typically what I've seen in books is this: If InvokeRequired Then...
5
by: Michael C# | last post by:
Hi all, I set up a System.Timers.Time in my app. The code basically just updates the screen, but since the processing performed is so CPU-intensive, I wanted to make sure it gets updated...
1
by: Mesan | last post by:
I'm getting a "Cross-thread operation not valid" Exception and I can't figure out why. I've got a BackgroundWorker that creates a UserControl with a whole lot of other user controls inside of...
1
by: Mark Denardo | last post by:
Hey all, I'm having a problem with the following: Private m_myDELEGATE As New myDELEGATE(AddressOf ReceivingServerMessage_UIThread) Delegate Sub myDELEGATE(ByVal ReceivingServerMessage As...
5
by: RobKinney1 | last post by:
Wow... unbelieveable that this problem would arise right before giving the software to our public testers... or maybe it is believable. We tweaked some seemingly unrelated code somewhere else...
4
by: Bill McCormick | last post by:
Hello, A timer control (I think) runs in another thread apart from a main form. In the OnTick event, you can update some form control with no worries. I'm making an AsyncronousServer class...
4
by: =?Utf-8?B?R3V5IENvaGVu?= | last post by:
Hi all I wrote a small demo program to understand timers and delegation - and its working fine. However, I would like to use the timer in a DLL(Class) When I compile the code: Protected...
1
by: Curious | last post by:
I have the following code: if (this.InvokeRequired) { IAsyncResult result = this.BeginInvoke(new EventHandler(this.UpdateButtons), new object { this, EventArgs.Empty }); while...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.