Help with PROCESS updating TREEVIEW

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
 
N

niceguy

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...
 

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