John,
You need to do the actual work in the DoWork event handler.
I would call LoadComputerDro pDown directly from DoWork, when
LoadComputerDro pDown completes I would Invoke a method on the UI that
"returns" the dataset...
Something like:
'Code
Private Delegate Sub LoadComputerDel egate(ByVal table As DataTable)
'called from form load
Protected Overrides Sub OnLoad(ByVal e As System.EventArg s)
MyBase.OnLoad(e )
tsStatus.Text = "Loading Computers. Please wait..."
bgLoadComputers .RunWorkerAsync ()
End Sub
'sub that does the work
Private Sub bgLoadComputers _DoWork(ByVal sender As System.Object, ByVal
e As System.Componen tModel.DoWorkEv entArgs) Handles bgLoadComputers .DoWork
System.Threadin g.Thread.Sleep( TimeSpan.FromSe conds(2))
LoadComputerDro pDown()
End Sub
Private Sub LoadComputerDro pDown()
Dim dtComputers As New DataTable("Comp uters")
dtComputers.Col umns.Add("Compu ter Name", GetType(String) )
'get the computers on the domain
Try
Dim enTry As DirectoryEntry = New
DirectoryEntry( "LDAP://TSBRADLEY")
Dim mySearcher As DirectorySearch er = New
DirectorySearch er(enTry)
mySearcher.Filt er = ("(objectClass= Computer)")
Dim resEnt As SearchResult
For Each resEnt In mySearcher.Find All
Try
Dim tmpID As String =
Mid(resEnt.GetD irectoryEntry.N ame.ToString, 4)
If Mid(tmpID, 1, 2) = "L1" Then
Dim rwComputer As DataRow
rwComputer = dtComputers.New Row
rwComputer("Com puter Name") =
(Mid((resEnt.Ge tDirectoryEntry ().Name.ToStrin g), 4))
dtComputers.Row s.Add(rwCompute r)
End If
Invoke(New LoadComputerDel egate(AddressOf
SetDomainComute rs), dtComputers)
Catch ex As Exception
Invoke(New LoadComputerDel egate(AddressOf
SetDomainComute rs), dtComputers)
End Try
Next
Catch ex As Exception
Invoke(New LoadComputerDel egate(AddressOf SetDomainComute rs),
dtComputers)
End Try
End Sub
Private Sub SetDomainComute rs(ByVal dtComputers As DataTable)
tsStatus.Text = "Computers Loaded..."
cboDomainComute rs.DataSource = dtComputers
cboDomainComute rs.DisplayMembe r = "Computer Name"
End Sub
Note: You should not refer to controls (tsStatus) from the DoWork method.
"John Wright" <ri**********@n otmail.comwrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
>I have a background worker thread that scans the network listing computers
that are connected. I have a delegate that I call that invokes a procdure
that loads a dataset with computer names and then attaches it to a
dropdown. When I run it, my UI freezes until it completes. Any thoughts on
how to do this without freezing the UI?
John
Code
'General Declaration
Private Delegate Sub LoadComputerDel egate()
'called from form load
bgLoadComputers .RunWorkerAsync ()
'sub that does the work
Private Sub bgLoadComputers _DoWork(ByVal sender As System.Object, ByVal e
As System.Componen tModel.DoWorkEv entArgs) Handles bgLoadComputers .DoWork
tsStatus.Text = "Loading Computers. Please wait..."
System.Threadin g.Thread.Sleep( TimeSpan.FromSe conds(2))
Invoke(New LoadComputerDel egate(AddressOf LoadComputerDro pDown))
End Sub
Private Sub LoadComputerDro pDown()
'get the computers on the domain
Try
Dim enTry As DirectoryEntry = New DirectoryEntry( LDAP://[MYDOMAIN])
Dim mySearcher As DirectorySearch er = New DirectorySearch er(enTry)
mySearcher.Filt er = ("(objectClass= Computer)")
Dim resEnt As SearchResult
For Each resEnt In mySearcher.Find All
Try
Dim tmpID As String = Mid(resEnt.GetD irectoryEntry.N ame.ToString, 4)
If Mid(tmpID, 1, 2) = "L1" Then
Dim rwComputer As DataRow
rwComputer = dtComputers.New Row
rwComputer("Com puter Name") =
(Mid((resEnt.Ge tDirectoryEntry ().Name.ToStrin g), 4))
dtComputers.Row s.Add(rwCompute r)
End If
tsStatus.Text = "Computers Loaded..."
cboDomainComute rs.DataSource = dtComputers
cboDomainComute rs.DisplayMembe r = "Computer Name"
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
End Sub