473,378 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

BackgroundWorker thread locking UI

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 LoadComputerDelegate()

'called from form load

bgLoadComputers.RunWorkerAsync()

'sub that does the work

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

tsStatus.Text = "Loading Computers. Please wait..."

System.Threading.Thread.Sleep(TimeSpan.FromSeconds (2))

Invoke(New LoadComputerDelegate(AddressOf LoadComputerDropDown))

End Sub

Private Sub LoadComputerDropDown()

'get the computers on the domain

Try

Dim enTry As DirectoryEntry = New DirectoryEntry(LDAP://[MYDOMAIN])

Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)

mySearcher.Filter = ("(objectClass=Computer)")

Dim resEnt As SearchResult

For Each resEnt In mySearcher.FindAll

Try

Dim tmpID As String = Mid(resEnt.GetDirectoryEntry.Name.ToString, 4)

If Mid(tmpID, 1, 2) = "L1" Then

Dim rwComputer As DataRow

rwComputer = dtComputers.NewRow

rwComputer("Computer Name") =
(Mid((resEnt.GetDirectoryEntry().Name.ToString), 4))

dtComputers.Rows.Add(rwComputer)

End If

tsStatus.Text = "Computers Loaded..."

cboDomainComuters.DataSource = dtComputers

cboDomainComuters.DisplayMember = "Computer Name"

Catch ex As Exception

End Try

Next

Catch ex As Exception

End Try

End Sub

Sep 19 '06 #1
3 3151
John,
You need to do the actual work in the DoWork event handler.

I would call LoadComputerDropDown directly from DoWork, when
LoadComputerDropDown completes I would Invoke a method on the UI that
"returns" the dataset...

Something like:

'Code
Private Delegate Sub LoadComputerDelegate(ByVal table As DataTable)

'called from form load

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
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.ComponentModel.DoWorkEventArgs) Handles bgLoadComputers.DoWork
System.Threading.Thread.Sleep(TimeSpan.FromSeconds (2))
LoadComputerDropDown()
End Sub

Private Sub LoadComputerDropDown()
Dim dtComputers As New DataTable("Computers")
dtComputers.Columns.Add("Computer Name", GetType(String))
'get the computers on the domain
Try
Dim enTry As DirectoryEntry = New
DirectoryEntry("LDAP://TSBRADLEY")
Dim mySearcher As DirectorySearcher = New
DirectorySearcher(enTry)
mySearcher.Filter = ("(objectClass=Computer)")
Dim resEnt As SearchResult
For Each resEnt In mySearcher.FindAll
Try
Dim tmpID As String =
Mid(resEnt.GetDirectoryEntry.Name.ToString, 4)
If Mid(tmpID, 1, 2) = "L1" Then
Dim rwComputer As DataRow
rwComputer = dtComputers.NewRow
rwComputer("Computer Name") =
(Mid((resEnt.GetDirectoryEntry().Name.ToString), 4))
dtComputers.Rows.Add(rwComputer)
End If
Invoke(New LoadComputerDelegate(AddressOf
SetDomainComuters), dtComputers)
Catch ex As Exception
Invoke(New LoadComputerDelegate(AddressOf
SetDomainComuters), dtComputers)
End Try
Next
Catch ex As Exception
Invoke(New LoadComputerDelegate(AddressOf SetDomainComuters),
dtComputers)
End Try
End Sub

Private Sub SetDomainComuters(ByVal dtComputers As DataTable)
tsStatus.Text = "Computers Loaded..."
cboDomainComuters.DataSource = dtComputers
cboDomainComuters.DisplayMember = "Computer Name"
End Sub

Note: You should not refer to controls (tsStatus) from the DoWork method.
"John Wright" <ri**********@notmail.comwrote in message
news:%2****************@TK2MSFTNGP05.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 LoadComputerDelegate()

'called from form load

bgLoadComputers.RunWorkerAsync()

'sub that does the work

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

tsStatus.Text = "Loading Computers. Please wait..."

System.Threading.Thread.Sleep(TimeSpan.FromSeconds (2))

Invoke(New LoadComputerDelegate(AddressOf LoadComputerDropDown))

End Sub

Private Sub LoadComputerDropDown()

'get the computers on the domain

Try

Dim enTry As DirectoryEntry = New DirectoryEntry(LDAP://[MYDOMAIN])

Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)

mySearcher.Filter = ("(objectClass=Computer)")

Dim resEnt As SearchResult

For Each resEnt In mySearcher.FindAll

Try

Dim tmpID As String = Mid(resEnt.GetDirectoryEntry.Name.ToString, 4)

If Mid(tmpID, 1, 2) = "L1" Then

Dim rwComputer As DataRow

rwComputer = dtComputers.NewRow

rwComputer("Computer Name") =
(Mid((resEnt.GetDirectoryEntry().Name.ToString), 4))

dtComputers.Rows.Add(rwComputer)

End If

tsStatus.Text = "Computers Loaded..."

cboDomainComuters.DataSource = dtComputers

cboDomainComuters.DisplayMember = "Computer Name"

Catch ex As Exception

End Try

Next

Catch ex As Exception

End Try

End Sub
Sep 20 '06 #2
John,
Doh! You don't need the delegate or the Invoke, the "beauty" of the
background work thread is that it handles the communication between both
threads for you.

Something like:

'called from form load

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
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.ComponentModel.DoWorkEventArgs) Handles bgLoadComputers.DoWork
System.Threading.Thread.Sleep(TimeSpan.FromSeconds (2))
e.Result = LoadComputerDropDown()
End Sub

Private Function LoadComputerDropDown() As DataTable
Dim dtComputers As New DataTable("Computers")
dtComputers.Columns.Add("Computer Name", GetType(String))
'get the computers on the domain
Dim enTry As DirectoryEntry = New DirectoryEntry("LDAP://[MYDOMAIN])
Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
mySearcher.Filter = ("(objectClass=Computer)")
Dim resEnt As SearchResult
For Each resEnt In mySearcher.FindAll
Dim tmpID As String =
Mid(resEnt.GetDirectoryEntry.Name.ToString, 4)
If Mid(tmpID, 1, 2) = "L1" Then
Dim rwComputer As DataRow
rwComputer = dtComputers.NewRow
rwComputer("Computer Name") =
(Mid((resEnt.GetDirectoryEntry().Name.ToString), 4))
dtComputers.Rows.Add(rwComputer)
End If
Next
Return dtComputers
End Function

Private Sub bgLoadComputers_RunWorkerCompleted(ByVal sender As Object,
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles
bgLoadComputers.RunWorkerCompleted
If e.Error Is Nothing Then
tsStatus.Text = "Computers Loaded..."
cboDomainComuters.DataSource = e.Result
cboDomainComuters.DisplayMember = "Computer Name"
Else
MessageBox.Show(e.Error.ToString(), Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net

"John Wright" <ri**********@notmail.comwrote in message
news:%2****************@TK2MSFTNGP05.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 LoadComputerDelegate()

'called from form load

bgLoadComputers.RunWorkerAsync()

'sub that does the work

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

tsStatus.Text = "Loading Computers. Please wait..."

System.Threading.Thread.Sleep(TimeSpan.FromSeconds (2))

Invoke(New LoadComputerDelegate(AddressOf LoadComputerDropDown))

End Sub

Private Sub LoadComputerDropDown()

'get the computers on the domain

Try

Dim enTry As DirectoryEntry = New DirectoryEntry(LDAP://[MYDOMAIN])

Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)

mySearcher.Filter = ("(objectClass=Computer)")

Dim resEnt As SearchResult

For Each resEnt In mySearcher.FindAll

Try

Dim tmpID As String = Mid(resEnt.GetDirectoryEntry.Name.ToString, 4)

If Mid(tmpID, 1, 2) = "L1" Then

Dim rwComputer As DataRow

rwComputer = dtComputers.NewRow

rwComputer("Computer Name") =
(Mid((resEnt.GetDirectoryEntry().Name.ToString), 4))

dtComputers.Rows.Add(rwComputer)

End If

tsStatus.Text = "Computers Loaded..."

cboDomainComuters.DataSource = dtComputers

cboDomainComuters.DisplayMember = "Computer Name"

Catch ex As Exception

End Try

Next

Catch ex As Exception

End Try

End Sub
Sep 20 '06 #3
Excellent. This works great. Thanks.

John
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:CD**********************************@microsof t.com...
John,
Doh! You don't need the delegate or the Invoke, the "beauty" of the
background work thread is that it handles the communication between both
threads for you.

Something like:

'called from form load

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
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.ComponentModel.DoWorkEventArgs) Handles bgLoadComputers.DoWork
System.Threading.Thread.Sleep(TimeSpan.FromSeconds (2))
e.Result = LoadComputerDropDown()
End Sub

Private Function LoadComputerDropDown() As DataTable
Dim dtComputers As New DataTable("Computers")
dtComputers.Columns.Add("Computer Name", GetType(String))
'get the computers on the domain
Dim enTry As DirectoryEntry = New
DirectoryEntry("LDAP://[MYDOMAIN])
Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
mySearcher.Filter = ("(objectClass=Computer)")
Dim resEnt As SearchResult
For Each resEnt In mySearcher.FindAll
Dim tmpID As String =
Mid(resEnt.GetDirectoryEntry.Name.ToString, 4)
If Mid(tmpID, 1, 2) = "L1" Then
Dim rwComputer As DataRow
rwComputer = dtComputers.NewRow
rwComputer("Computer Name") =
(Mid((resEnt.GetDirectoryEntry().Name.ToString), 4))
dtComputers.Rows.Add(rwComputer)
End If
Next
Return dtComputers
End Function

Private Sub bgLoadComputers_RunWorkerCompleted(ByVal sender As Object,
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles
bgLoadComputers.RunWorkerCompleted
If e.Error Is Nothing Then
tsStatus.Text = "Computers Loaded..."
cboDomainComuters.DataSource = e.Result
cboDomainComuters.DisplayMember = "Computer Name"
Else
MessageBox.Show(e.Error.ToString(), Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net

"John Wright" <ri**********@notmail.comwrote in message
news:%2****************@TK2MSFTNGP05.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 LoadComputerDelegate()

'called from form load

bgLoadComputers.RunWorkerAsync()

'sub that does the work

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

tsStatus.Text = "Loading Computers. Please wait..."

System.Threading.Thread.Sleep(TimeSpan.FromSecond s(2))

Invoke(New LoadComputerDelegate(AddressOf LoadComputerDropDown))

End Sub

Private Sub LoadComputerDropDown()

'get the computers on the domain

Try

Dim enTry As DirectoryEntry = New DirectoryEntry(LDAP://[MYDOMAIN])

Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)

mySearcher.Filter = ("(objectClass=Computer)")

Dim resEnt As SearchResult

For Each resEnt In mySearcher.FindAll

Try

Dim tmpID As String = Mid(resEnt.GetDirectoryEntry.Name.ToString, 4)

If Mid(tmpID, 1, 2) = "L1" Then

Dim rwComputer As DataRow

rwComputer = dtComputers.NewRow

rwComputer("Computer Name") =
(Mid((resEnt.GetDirectoryEntry().Name.ToString) , 4))

dtComputers.Rows.Add(rwComputer)

End If

tsStatus.Text = "Computers Loaded..."

cboDomainComuters.DataSource = dtComputers

cboDomainComuters.DisplayMember = "Computer Name"

Catch ex As Exception

End Try

Next

Catch ex As Exception

End Try

End Sub

Sep 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
2
by: Sebastian Crewe | last post by:
Greetings, I was much encouraged to see the new BackgroundWorker class in .NET v2. On the face of it, much easier to use than the various delegates and events of yore, though I imagine the same...
5
by: Rob R. Ainscough | last post by:
I'm using a BackgroundWorker to perform a file download from an ftp site. Per good code design practices where I separate my UI code from my core logic code (in this case my Download file method in...
9
by: =?Utf-8?B?VE9NX1Bhc2FkZW5h?= | last post by:
Hello, In my ASP.Net app I'm launching a BackgroundWorker thread in my Page_Load function. In that thread I'm attempting to connect to a SQL server using this connection string "Initial...
5
by: redear | last post by:
Is there a way to immediately terminate a BackgroundWorker thread? My problem is that the BackgroundWorker starts with a call to My.Computer.FileSystem.GetFiles that can run for a very long time if...
8
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
I have an application with several BackgroundWorker threads. I hoped I'd be able to just type backgroundworker1.Name = "bw1"; but I don't see a name property. Any thoughts on how to name a...
9
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
5
by: =?Utf-8?B?RWl0YW4=?= | last post by:
Hello, I need to create a background thread and two (or more) options are available to me: 1. BackgroundWorker 2. Asynch delegate method and BeginInvoke What are the differences between...
4
by: Sin Jeong-hun | last post by:
This is what I've always been wondered. Suppose I've created a class named Agent, and the Agent does some lengthy job. Of course I don't want to block the main window, so the Agent does the job in...
2
by: =?Utf-8?B?SGFycnkgS2Vjaw==?= | last post by:
I have introduced a component to my solution that is throwing an exception about needing to be run in single threaded apartment mode. This component is created in an async call by a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.