By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,009 Members | 2,866 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,009 IT Pros & Developers. It's quick & easy.

Correct thread termination

P: n/a
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 ManualResetEvent'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 ManualResetEvent(False)
Private cMREReady As New ManualResetEvent(True)
Private cMRETerminate As New ManualResetEvent(False)

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
Call startThread(e.Location)
End Sub

Private Sub startThread(ByVal iParam As Object, Optional ByVal iJustAbort As
Boolean = False)
If (cMREBusy.WaitOne(0, False)) Then
Call Console.WriteLine("Thread has already started...")
Call cMRETerminate.Set()
If (cMREReady.WaitOne(3000, False)) Then
Call Console.WriteLine("Thread terminated!")
If (Not iJustAbort) Then
GoTo startThreadProcess
End If
Else
Call Console.WriteLine("Could not terminate thread!")
End If
Else
startThreadProcess:
Call Console.WriteLine("Starting thread!")
Call cMREReady.Reset() '//We are not ready to start again
Call cMREBusy.Set() '//We are busy now
Call cMRETerminate.Reset() '//We can be terminated again from now
pTrdThread = New Thread(New ParameterizedThreadStart(AddressOf
thread_Callback))
Call pTrdThread.Start(iParam)
End If
End Sub

Private Sub thread_Callback(ByVal iParam As Object)
Call Console.WriteLine("Thread started with - " & iParam.ToString & " @
" & Environment.TickCount)

Dim pGraGraphics As Graphics = PictureBox1.CreateGraphics()
Using pGraGraphics

Dim pIntCurItem As Integer
For pIntCurItem = 0 To 60
Call pGraGraphics.DrawLine(Pens.Black, CType(iParam, Point),
Point.Add(CType(iParam, Point), New Size(1, 1)))
Call Thread.Sleep(100)
If (cMRETerminate.WaitOne(0, False)) Then
Call Console.WriteLine("Thread terminating @ " &
Environment.TickCount)
Exit For
End If
Next

End Using

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

-------
Mar 2 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.