should i use thread.abort to stop code execution?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt

    #1

    should i use thread.abort to stop code execution?

    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(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    Me.Show()

    thread = New System.Threadin g.Thread(Addres sOf
    Me.UpdateCheckL istStatus)

    thread.Start()
    End Sub

    Private Function CreateMessage(B yVal pastDueUsers As UserCollection) As
    String
    'do something here
    End Function

    Private Sub UpdateCheckList Status()
    try
    Dim u As New UserCollection
    Dim message As String
    Dim email As New anEmail.cStore
    Dim recipients(0) As String
    Dim pastDueUnderwri ters As New UserCollection
    Dim pastDueAssociat es As New UserCollection
    Dim pastDueOtherUse rs As New UserCollection

    ProgressBar1.St ep = 1

    ProgressBar1.Mi nimum = 0

    ProgressBar1.Ma ximum = 7

    pastdueItem.Upd ateCheckListSta tus(Configurati onSettings.AppS ettings("PastDu eDays"))

    ProgressBar1.Pe rformStep()

    u =
    user.GetAllUser s(Configuration Settings.AppSet tings("PastDueD ays"))

    ProgressBar1.Pe rformStep()

    For Each user As User In u
    If user.hasPastDue Items Then
    If user.Title = Title.ASSOCIATE Then
    pastDueAssociat es.Add(user)
    ElseIf user.Title = Title.UNDERWRIT ER Then
    pastDueUnderwri ters.Add(user)
    Else
    pastDueOtherUse rs.Add(user)
    End If


    For Each pastdueItem As PastDueItem In user.PastDueIte ms
    message += String.Format(" {0} [{1}] {2}",
    pastdueItem.Pol icyNumber, pastdueItem.Bus inessType,
    pastdueItem.Rec eivedDate.ToStr ing("d")) & vbCrLf
    Next

    recipients(0) = String.Format(" {0}@anpac.com", user.ID)

    'email.StoreMes sage(recipients ,
    "UpdateCheckLis tStatus@anpac.c om", "Past Due Items", message)
    End If
    Next

    ProgressBar1.Pe rformStep()

    pastDueAssociat es.Sort(UserSor tType.Division)
    pastDueUnderwri ters.Sort(UserS ortType.Divisio n)
    pastDueOtherUse rs.Sort(UserSor tType.Division)

    ProgressBar1.Pe rformStep()

    message = CreateMessage(p astDueAssociate s)

    If Not message = String.Empty Then
    For Each user As User In u
    If user.Title = Title.SUPERVISO R Then
    'email.StoreMes sage(recipients ,
    "UpdateCheckLis tStatus@anpac.c om", "Past Due Items", message)
    End If
    Next
    End If

    ProgressBar1.Pe rformStep()

    message = String.Empty

    message = CreateMessage(p astDueUnderwrit ers)

    If Not message = String.Empty Then
    For Each user As User In u
    If user.Title = Title.SUPERVISO R Then
    'email.StoreMes sage(recipients ,
    "UpdateCheckLis tStatus@anpac.c om", "Past Due Items", message)
    End If
    Next
    End If

    ProgressBar1.Pe rformStep()

    message = String.Empty

    message = CreateMessage(p astDueOtherUser s)

    If Not message = String.Empty Then
    'email.StoreMes sage(recipients ,
    "UpdateCheckLis tStatus@anpac.c om", "Past Due Items", message)
    End If

    ProgressBar1.Pe rformStep()
    catch ex as exception
    msgbox(ex.messa ge)
    end try
    End Sub

    Private Sub btnCancel_Click (ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles btnCancel.Click
    thread.Abort()
    End Sub


Working...