Correct thread termination

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

    #1

    Correct thread termination

    Hi there,

    Previously when handling the termination of threads I had just called the
    abort method and caught it in a try catch block in the thread callback.
    After recently learning that this was a bad way of handling thread
    termination I am working on terminating threads by using ManualResetEven t's.

    I have come up with the following code which appears to work very well (at
    end of message). I would like to know if anyone knows of any problems that
    I could encounter from this code and whether or not it is following correct
    procedure.

    If someone has a link to suitable reference I would be most appreciative if
    you could point me toward it. Otherwise, does this code look okay?

    SSA

    -------

    Private pTrdThread As Thread
    Private cMREBusy As New ManualResetEven t(False)
    Private cMREReady As New ManualResetEven t(True)
    Private cMRETerminate As New ManualResetEven t(False)

    Private Sub PictureBox1_Mou seMove(ByVal sender As Object, ByVal e As
    System.Windows. Forms.MouseEven tArgs) Handles PictureBox1.Mou seMove
    Call startThread(e.L ocation)
    End Sub

    Private Sub startThread(ByV al iParam As Object, Optional ByVal iJustAbort As
    Boolean = False)
    If (cMREBusy.WaitO ne(0, False)) Then
    Call Console.WriteLi ne("Thread has already started...")
    Call cMRETerminate.S et()
    If (cMREReady.Wait One(3000, False)) Then
    Call Console.WriteLi ne("Thread terminated!")
    If (Not iJustAbort) Then
    GoTo startThreadProc ess
    End If
    Else
    Call Console.WriteLi ne("Could not terminate thread!")
    End If
    Else
    startThreadProc ess:
    Call Console.WriteLi ne("Starting thread!")
    Call cMREReady.Reset () '//We are not ready to start again
    Call cMREBusy.Set() '//We are busy now
    Call cMRETerminate.R eset() '//We can be terminated again from now
    pTrdThread = New Thread(New ParameterizedTh readStart(Addre ssOf
    thread_Callback ))
    Call pTrdThread.Star t(iParam)
    End If
    End Sub

    Private Sub thread_Callback (ByVal iParam As Object)
    Call Console.WriteLi ne("Thread started with - " & iParam.ToString & " @
    " & Environment.Tic kCount)

    Dim pGraGraphics As Graphics = PictureBox1.Cre ateGraphics()
    Using pGraGraphics

    Dim pIntCurItem As Integer
    For pIntCurItem = 0 To 60
    Call pGraGraphics.Dr awLine(Pens.Bla ck, CType(iParam, Point),
    Point.Add(CType (iParam, Point), New Size(1, 1)))
    Call Thread.Sleep(10 0)
    If (cMRETerminate. WaitOne(0, False)) Then
    Call Console.WriteLi ne("Thread terminating @ " &
    Environment.Tic kCount)
    Exit For
    End If
    Next

    End Using

    Call Console.WriteLi ne("Thread exited @ " & Environment.Tic kCount)
    Call cMREReady.Set() '//We are now ready to start again
    Call cMREBusy.Reset( ) '//We are not busy anymore
    End Sub

    -------


Working...