By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,701 Members | 1,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,701 IT Pros & Developers. It's quick & easy.

Treeview and Delegate stuff...

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.