I'm attempting to load a form, query a database, and update a progress bar
on the same form. I want the form to load and activate/enable the form's
cancel button while the code continues to execute and the progress bar
updates. The user will be able to press the cancel button to stop
execution. Everything works fine, but I've noticed that when I call
thread.Abort, an exception is thrown: "Thread was being aborted."
Is there a better way to accomplish this task? (see code below)
Thanks!
Matt
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Show()
thread = New System.Threading.Thread(AddressOf
Me.UpdateCheckListStatus)
thread.Start()
End Sub
Private Function CreateMessage(ByVal pastDueUsers As UserCollection) As
String
'do something here
End Function
Private Sub UpdateCheckListStatus()
try
Dim u As New UserCollection
Dim message As String
Dim email As New anEmail.cStore
Dim recipients(0) As String
Dim pastDueUnderwriters As New UserCollection
Dim pastDueAssociates As New UserCollection
Dim pastDueOtherUsers As New UserCollection
ProgressBar1.Step = 1
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = 7
pastdueItem.UpdateCheckListStatus(ConfigurationSet tings.AppSettings("PastDueDays"))
ProgressBar1.PerformStep()
u =
user.GetAllUsers(ConfigurationSettings.AppSettings ("PastDueDays"))
ProgressBar1.PerformStep()
For Each user As User In u
If user.hasPastDueItems Then
If user.Title = Title.ASSOCIATE Then
pastDueAssociates.Add(user)
ElseIf user.Title = Title.UNDERWRITER Then
pastDueUnderwriters.Add(user)
Else
pastDueOtherUsers.Add(user)
End If
For Each pastdueItem As PastDueItem In user.PastDueItems
message += String.Format("{0} [{1}] {2}",
pastdueItem.PolicyNumber, pastdueItem.BusinessType,
pastdueItem.ReceivedDate.ToString("d")) & vbCrLf
Next
recipients(0) = String.Format("{0*@anpac.com", user.ID)
'email.StoreMessage(recipients,
"Up*******************@anpac.com", "Past Due Items", message)
End If
Next
ProgressBar1.PerformStep()
pastDueAssociates.Sort(UserSortType.Division)
pastDueUnderwriters.Sort(UserSortType.Division)
pastDueOtherUsers.Sort(UserSortType.Division)
ProgressBar1.PerformStep()
message = CreateMessage(pastDueAssociates)
If Not message = String.Empty Then
For Each user As User In u
If user.Title = Title.SUPERVISOR Then
'email.StoreMessage(recipients,
"Up*******************@anpac.com", "Past Due Items", message)
End If
Next
End If
ProgressBar1.PerformStep()
message = String.Empty
message = CreateMessage(pastDueUnderwriters)
If Not message = String.Empty Then
For Each user As User In u
If user.Title = Title.SUPERVISOR Then
'email.StoreMessage(recipients,
"Up*******************@anpac.com", "Past Due Items", message)
End If
Next
End If
ProgressBar1.PerformStep()
message = String.Empty
message = CreateMessage(pastDueOtherUsers)
If Not message = String.Empty Then
'email.StoreMessage(recipients,
"Up*******************@anpac.com", "Past Due Items", message)
End If
ProgressBar1.PerformStep()
catch ex as exception
msgbox(ex.message)
end try
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCancel.Click
thread.Abort()
End Sub