Treeview and Delegate stuff...

J

Jarod_24

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?
 
I

Imran Koradia

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/multithreading.html#windows.forms (this
one's in C# but you should get the idea)


hope that helps..
Imran.
 
J

Jarod_24

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
 
I

Imran Koradia

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 said:
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
treeStatus.Nodes.Add(tNode) from a different thread than the one that
created the
 
G

Guest

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

(e-mail address removed)
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/)
 
R

Robin Tucker

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top