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

Treeview and Delegate stuff...

This is the exception i'm getting:

System.InvalidOperationException: 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.
I have multiple background-threads that returns info about their progress through a Event. This event's handler is then on a Form
that has the responsability to display all the info to the user through a Treeview control.

Earlier in the development i used a textbox to see the data, and it worked without a hitch, but a treeview is a more practical
display.

The exception occurs when i try to add the nodes genereated from the recieved data (an array with some strings) to the Treeview
control itself:

treeStatus.Nodes.Add(tNode)

tNode is a node btw with some subnodes, nothing strange there.

That's when the exception occurs, but i still dont understand how to solve this?
Nov 21 '05 #1
6 4349
You can manipulate windows forms controls only from the thread that created
the controls which is almost always the main application thread. This error
message means you're trying to manipulate a control from another thread. The
only methods you can call on a control from another thread are the
Control.Invoke, Control.BeingInvoke and Control.EndInvoke (and I think
CreateGraphics also). I believe you're calling treeStatus.Nodes.Add(tNode)
from a different thread than the one that created the treeview. A couple of
articles on how to use delegates and Control.Invoke:
http://www.devx.com/dotnet/Article/11358
http://www.yoda.arachsys.com/csharp/...#windows.forms (this
one's in C# but you should get the idea)
hope that helps..
Imran.

"Jarod_24" <ja******@hotmail.com> wrote in message
news:1096843403.JPcjOv9ssfBVvy4qQKwtpg@teranews...
This is the exception i'm getting:

System.InvalidOperationException: 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.
I have multiple background-threads that returns info about their progress
through a Event. This event's handler is then on a Form that has the
responsability to display all the info to the user through a Treeview
control.

Earlier in the development i used a textbox to see the data, and it worked
without a hitch, but a treeview is a more practical display.

The exception occurs when i try to add the nodes genereated from the
recieved data (an array with some strings) to the Treeview control itself:

treeStatus.Nodes.Add(tNode)

tNode is a node btw with some subnodes, nothing strange there.

That's when the exception occurs, but i still dont understand how to solve
this?

Nov 21 '05 #2
In addition to Imran,

In my opinion is the current information about threads on MSDN very good.
http://msdn.microsoft.com/library/de...isualBasic.asp

Your problem and solution is as well described there.

I hope this helps as well?

Cor
"Jarod_24" <ja******@hotmail.com>
This is the exception i'm getting:

System.InvalidOperationException: 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.
I have multiple background-threads that returns info about their progress
through a Event. This event's handler is then on a Form that has the
responsability to display all the info to the user through a Treeview
control.

Earlier in the development i used a textbox to see the data, and it worked
without a hitch, but a treeview is a more practical display.

The exception occurs when i try to add the nodes genereated from the
recieved data (an array with some strings) to the Treeview control itself:

treeStatus.Nodes.Add(tNode)

tNode is a node btw with some subnodes, nothing strange there.

That's when the exception occurs, but i still dont understand how to solve
this?

Nov 21 '05 #3
Strange.
I have made a "Threader" class (in the main thread) that has the responsability to create the threads, adjust priority etc.
All the threads are given a referance back to the Threader class when created, and they use this to ask for permissions to access
resources (files) and to call a method that will raise an event.

I thought that the data was passed on from Thread X to the main thread through this referance to "Threader".
And then "Threader" sends it on to the Form (wich is also a part of the main thread)?
As i said earlier; i had no problems with this when using a textbox.
Here's some example code to show how i've done it.

Public Class Threader
....
Public sub NewThread()
Dim child as new ThreadChild(Me)
Dim t as new Threading.Thread(AddressOf child.Main)
t.ApartmentState = Threading.ApartmentState.MTA
t.Start
End sub

public sub getEventdata(byval list() as string)
'this method is called by the individual threads (no serialization there though)
RaiseEvent MyThreadEvent(list)
end sub

End Class

Public class ThreadChild
private MyOwner at threader ' referance set in New()

public sub Main()
'does some stuff here
...
call MyOwner.getEventdata(mylist)
end sub

End Class

"Imran Koradia" <no****@microsoft.com> wrote in message news:uE*************@TK2MSFTNGP11.phx.gbl...
You can manipulate windows forms controls only from the thread that created the controls which is almost always the main
application thread. This error message means you're trying to manipulate a control from another thread. The only methods you can
call on a control from another thread are the Control.Invoke, Control.BeingInvoke and Control.EndInvoke (and I think
CreateGraphics also). I believe you're calling treeStatus.Nodes.Add(tNode) from a different thread than the one that created the
treeview. A couple of articles on how to use delegates and Control.Invoke:
http://www.devx.com/dotnet/Article/11358
http://www.yoda.arachsys.com/csharp/...#windows.forms (this one's in C# but you should get the idea)
hope that helps..
Imran.

"Jarod_24" <ja******@hotmail.com> wrote in message news:1096843403.JPcjOv9ssfBVvy4qQKwtpg@teranews...
This is the exception i'm getting:

System.InvalidOperationException: 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.
I have multiple background-threads that returns info about their progress through a Event. This event's handler is then on a Form
that has the responsability to display all the info to the user through a Treeview control.

Earlier in the development i used a textbox to see the data, and it worked without a hitch, but a treeview is a more practical
display.

The exception occurs when i try to add the nodes genereated from the recieved data (an array with some strings) to the Treeview
control itself:

treeStatus.Nodes.Add(tNode)

tNode is a node btw with some subnodes, nothing strange there.

That's when the exception occurs, but i still dont understand how to solve this?


Nov 21 '05 #4
I don't see any of your controls in this code. Where are you trying to add
the nodes to the treeview? Also, how what was the code with the textbox that
you got it to work with?

Imran.

"Jarod_24" <ja******@hotmail.com> wrote in message
news:1096882801.EVEqYYvhQ0oK4HG4ipEZiw@teranews...
Strange.
I have made a "Threader" class (in the main thread) that has the responsability to create the threads, adjust priority etc. All the threads are given a referance back to the Threader class when created, and they use this to ask for permissions to access resources (files) and to call a method that will raise an event.

I thought that the data was passed on from Thread X to the main thread through this referance to "Threader". And then "Threader" sends it on to the Form (wich is also a part of the main thread)? As i said earlier; i had no problems with this when using a textbox.
Here's some example code to show how i've done it.

Public Class Threader
....
Public sub NewThread()
Dim child as new ThreadChild(Me)
Dim t as new Threading.Thread(AddressOf child.Main)
t.ApartmentState = Threading.ApartmentState.MTA
t.Start
End sub

public sub getEventdata(byval list() as string)
'this method is called by the individual threads (no serialization there though) RaiseEvent MyThreadEvent(list)
end sub

End Class

Public class ThreadChild
private MyOwner at threader ' referance set in New()

public sub Main()
'does some stuff here
...
call MyOwner.getEventdata(mylist)
end sub

End Class

"Imran Koradia" <no****@microsoft.com> wrote in message

news:uE*************@TK2MSFTNGP11.phx.gbl...
You can manipulate windows forms controls only from the thread that created the controls which is almost always the main
application thread. This error message means you're trying to manipulate a control from another thread. The only methods you can call on a control from another thread are the Control.Invoke, Control.BeingInvoke and Control.EndInvoke (and I think CreateGraphics also). I believe you're calling treeStatus.Nodes.Add(tNode) from a different thread than the one that
created the treeview. A couple of articles on how to use delegates and Control.Invoke: http://www.devx.com/dotnet/Article/11358
http://www.yoda.arachsys.com/csharp/...#windows.forms (this one's in C# but you should get the idea)

hope that helps..
Imran.

"Jarod_24" <ja******@hotmail.com> wrote in message

news:1096843403.JPcjOv9ssfBVvy4qQKwtpg@teranews...
This is the exception i'm getting:

System.InvalidOperationException: 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.

I have multiple background-threads that returns info about their progress through a Event. This event's handler is then on a Form that has the responsability to display all the info to the user through a Treeview control.
Earlier in the development i used a textbox to see the data, and it worked without a hitch, but a treeview is a more practical display.

The exception occurs when i try to add the nodes genereated from the recieved data (an array with some strings) to the Treeview control itself:

treeStatus.Nodes.Add(tNode)

tNode is a node btw with some subnodes, nothing strange there.

That's when the exception occurs, but i still dont understand how to solve this?



Nov 21 '05 #5
Hi,

I have the same problem that you have had, I did try to use invoke methods but this time I receive target invocation errors. Anyway to get over this?

Onder

on***************@mikes.com.tr
This is the exception i'm getting:

System.InvalidOperationException: 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.
I have multiple background-threads that returns info about their progress through a Event. This event's handler is then on a Form
that has the responsability to display all the info to the user through a Treeview control.

Earlier in the development i used a textbox to see the data, and it worked without a hitch, but a treeview is a more practical
display.

The exception occurs when i try to add the nodes genereated from the recieved data (an array with some strings) to the Treeview
control itself:

treeStatus.Nodes.Add(tNode)

tNode is a node btw with some subnodes, nothing strange there.

That's when the exception occurs, but i still dont understand how to solve this?


User submitted from AEWNET (http://www.aewnet.com/)
Nov 21 '05 #6
Can I just say, that if you are using threads, you would be well advised to
execute delegates using Invoke, rather than by just raising an event. The
Invoke method will marshal it's execution onto the same thread as the object
you are invoking on.

"Guest" <Guest@aew_nospam.com> wrote in message
news:eW**************@TK2MSFTNGP10.phx.gbl...
Hi,

I have the same problem that you have had, I did try to use invoke methods
but this time I receive target invocation errors. Anyway to get over this?

Onder

on***************@mikes.com.tr
This is the exception i'm getting:

System.InvalidOperationException: 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.
I have multiple background-threads that returns info about their progress
through a Event. This event's handler is then on a Form
that has the responsability to display all the info to the user through a
Treeview control.

Earlier in the development i used a textbox to see the data, and it
worked without a hitch, but a treeview is a more practical
display.

The exception occurs when i try to add the nodes genereated from the
recieved data (an array with some strings) to the Treeview
control itself:

treeStatus.Nodes.Add(tNode)

tNode is a node btw with some subnodes, nothing strange there.

That's when the exception occurs, but i still dont understand how to
solve this?


User submitted from AEWNET (http://www.aewnet.com/)

Nov 21 '05 #7

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

Similar topics

0
by: serge calderara | last post by:
Dear all, I have an VB windows application that colect information form a remote database and then populate the tree view with part of the reords. As database data can be huge , I have used the...
0
by: Mike Preston | last post by:
Warning - long message. Many thanks for making the A97 version of the treeview w/o activex controls available. I have taken it down a different path and ended up doing almost everything...
1
by: Mitch A | last post by:
I have an app that populates a treeview from a DB. I would like the UI to repaint during this potentially long operation, so I want to spawn a new thread to populate the list. The problem is how...
0
by: Bobby Owens | last post by:
Hi, I'm current having a problem with the treeview control and multi threading. The treeview is on a form. A request is then sent to a server using IP for the data. The data arrives in an event...
0
by: Henry | last post by:
I am trying to create a TreeView control that works with an ADO Dataset DataTable or the new BindingSource stuff in .NET 2.0 to build a Treeview that is populated. This is what I came up with...
1
by: Dean Hinson | last post by:
Hello, I am trying to spawn a sunroutine in a worker thread for populating a trreeview. I set the selectednode and then start the thread. However, the thread errors because the selectednode was...
2
by: casManG | last post by:
I am working on a small project that uses the treeview control in .net 2003. I have a tree view that I am sending to a sub in order to iterate through the nodes. Public Sub test (ByVal...
3
by: bg_ie | last post by:
Hi, Lets say I have a TreeView with nodes that represent objects of a number of different types. Is there a way of associating the Set method of each object with its node in the TreeView? That...
0
by: Falcula | last post by:
Hello, I have a treeview that i fill from a database, when i update nodename in database the treeview dont update. Its works when iam not useing enableviewstate="true" but then i loosing the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.