Cancelling previous thread

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • machado
    New Member
    • Dec 2009
    • 2

    Cancelling previous thread

    Greetings.
    I have a program in VB2008 and I need to execute a thread from differents Subs.
    Each time I start the thread I want to cancel the previous.
    Here an example:
    Code:
    Private Sub TextBox_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs)
               Dim ThreadResults As New Thread(AddressOf ShowResults)
               ThreadResults.Start()
    End Sub
    
    
    Private Sub ckBoxAllRecords_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
               Dim ThreadResults As New Thread(AddressOf ShowResults)
               ThreadResults.Start()
    End Sub
    I write in a textbox, TextBox_Changed fires, thread spend 4 minutes filling a dataset
    I check a checkbox, ckBoxAllRecords _CheckedChanged fires, thread spend 10 seconds filling a dataset.
    I dont want anymore the dataset of TextBox_Changed so I want to cancel its filling.
    It is possible to do?
    Thanks in advance for any reply
    Last edited by Frinavale; Dec 10 '09, 05:07 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    If you have an object reference to the Thread you could call the Abort() function, I believe that is frowned upon though. In favor of changing some boolean variable that tells the thread to exit itself.

    Comment

    • machado
      New Member
      • Dec 2009
      • 2

      #3
      Thanks for your help.
      I resolved my problem creating a thread array.

      Code:
              Private ThreadResults(4) As Thread
      .
      .
              CancelThreads()
              ThreadResults(0) = New Thread(AddressOf Me.ShowResults)
              ThreadResults(0).Start()
      .
      .
      Private Sub CancelThreads()
              For i = 0 To UBound(ThreadResults)
                  Try
                      If ThreadResults(i).IsAlive Then
                          ThreadResults(i).Abort()
                      End If
                  Catch
                  End Try
              Next
       End Sub

      Comment

      Working...