threading problem

D

Dzemo

i try to implement simple threading im my application and i get this error
message :

An unhandled exception of type 'System.InvalidOperationException' occurred
in system.windows.forms.dll
Additional information: 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.

on this line on one of two threads that i have in my application:
tree.Nodes.Add(Nodex)


TreeView is declared on form not on current thread. Why, any ideas? Any
examples any ideas Any help anything
THX
 
H

Herfried K. Wagner [MVP]

Dzemo said:
An unhandled exception of type 'System.InvalidOperationException' occurred
in system.windows.forms.dll
Additional information: 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.

on this line on one of two threads that i have in my application:
tree.Nodes.Add(Nodex)


TreeView is declared on form not on current thread. Why, any ideas?

You must not access instance members of Windows Forms controls from another
thread because they are not safe for multithreading.

Multithreading in Windows Forms applications
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithreading&lang=en>

\\\
Private Delegate Function AddDelegate( _
ByVal text As String _
) As TreeNode

Private Sub DoWork()
Me.TreeView1.Invoke( _
New AddDelegate(AddressOf Me.TreeView1.Nodes.Add), _
New Object() {"Bla"} _
)
End Sub
..
..
..
Dim th As New Thread(AddressOf DoWork)
th.Start()
///

Related: 'Control.InvokeRequired'/'Control.BeginInvoke'.
 

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