Help with PROCESS updating TREEVIEW

  • Thread starter Thread starter John Cosmas
  • Start date Start date
J

John Cosmas

I've developed a THREAD, which takes it time and does some inquiries for me
from a database. Then, I've writing the results into a TREEVIEW, but the
THREAD code generates an error - "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" - whenever it tries to populate the TREEVIEW nodes.

My code does this....

Sub Form_Load...

Dim pobj100Thread As Thread
pobj100Thread = New Thread(AddressOf ps_List_Servers)
pobj100Thread.Start()

End Sub

Private Sub ps_List_Servers()

Dim pobjSQLApp As New SQLDMO.Application
Dim pobjSQLNames As SQLDMO.NameList
Dim pint300Temp As Integer
Dim pint350Temp As Integer = 0

pobjSQLNames = pobjSQLApp.ListAvailableSQLServers
With Me.ctlLogsTreeVw
.BeginUpdate()
For pint300Temp = 1 To pobjSQLNames.Count
If pobjSQLNames.Item(pint300Temp) <> "(local)" Then
.Nodes(0).Nodes.Add(New
TreeNode(pobjSQLNames.Item(pint300Temp)))
.Nodes(0).Nodes(pint350Temp).Tag = "Server"
pint350Temp = pint350Temp + 1
End If
Next
.EndUpdate()
End With

End Sub
 
Hi John,

The exception is happening because you are trying to do UI stuff on a
control on a thread that is not the thread on which the control was created.

Check the online help for the InvokeRequired property:

If me.InvokeRequired Then
' use the right thread ...
me.BeginInvoke(someDelegate(new object(){sender, e})
Else
' DoSomething on the current thread ...
' ...
End If


Hope this helps...
 
Back
Top