Using the backgroundworker to populate a treeview

J

john wright

I have my code done to populate a treeview, but when I move it to the
backgroundworker I get an error "Action being performed on this control is
being called from the wrong thread. Marshal to the correct thread using
Control.Invoke or Control.BeginInvoke to perform this action (code to
follow). What am I doing wrong. I moved the LoadWIP function to the
backgroundworker DoWork method instead of calling the sub, but got the same
problem. I am trying to get the background worker to load the treeview then
I will have another background worker maintain the treeview by removing and
adding nodes to the tree every minute. Any help would be appreciated.

John


Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e
As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

LoadWIP()

'load the steps

' LoadWIPParts(ds.Tables(0))

End Sub

Private Sub LoadWIP()

Dim ds As New DataSet

Dim WIP As New localhost.WIP

ds = WIP.GetTravelerByStation(8)

Dim masterRow As DataRow

masterRow = ds.Tables(0).Rows(0)

'Add the first row cell

Dim masterNode As New TreeNode(masterRow("Station_Name").ToString())

TreeView1.Nodes.Add(masterNode)-------------> This line is giving the
failure

masterNode.ImageIndex = 6

masterNode.SelectedImageIndex = 6

'Go through the next rows and add the steps

For Each childRow As DataRow In ds.Tables(0).Rows

Dim childNode As New TreeNode("Step: " & childRow("Step").ToString())

masterNode.Nodes.Add(childNode)

childNode.ImageIndex = 6

childNode.SelectedImageIndex = 6

Next

'load the steps

LoadWIPParts(ds.Tables(0))
 
A

Armin Zingler

john wright said:
I have my code done to populate a treeview, but when I move it to
the backgroundworker I get an error "Action being performed on this
control is being called from the wrong thread. Marshal to the
correct thread using Control.Invoke or Control.BeginInvoke to
perform this action (code to follow). What am I doing wrong.

The message says what you're doing wrong and how to do it right.


See also:
http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconFreeThreadingWithFormsControls.asp

http://msdn.microsoft.com/library/en-us/dnforms/html/winforms06112002.asp

http://msdn.microsoft.com/library/e...evelopingmultithreadedwindowsformscontrol.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet09272002.asp


Armin
 

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