Thread Abort and SyncLock

  • Thread starter Thread starter fred
  • Start date Start date
F

fred

I use a Synclock in a secondary thread and also stop the thread using the
abort method.
If the abort occurs while the thread is in the Synclock will the SyncLock
always be released before the thread stops or do I need to add some extra
code to avoid Synclock staying on after the thread has been stopped.

Thanks
Fred
 
Fred,
You should be safe, as the Synclock contains an implicit Try/Finally block.

Within the Synclocks implicit Finally Block it releases the lock on the
object.

Thread.Abort actually raises the ThreadAbortException, the finally block
will be entered...

Note, rather then using the Thread.Abort method to stop a thread, you should
consider a more Thread friendly method. Such as an AutoResetEvent or
ManualResetEvent.

Hope this helps
Jay
 
Thanks for your reply Jay. The reason I used Abort was because my
application hung whenever I tried to cancel the thread using
ManualResetEvent.
Originally I had a Cancel routine as below. My thread had numerous "If
CancelDocThread.WaitOne(0, False) then ..." to stop the routine if
cancelled.
The problem was that it would alway hang whenever I run this and think I
have just figured out why. Perhaps you or some else can confirm this for me.

My thread adds nodes to a TreeView but to do this from another thread I had
to use the Invoke method. Am I correct in thinking that the Invoke method
then tries to run the delegate method in the main thread? If so what happens
if the main thread is waiting for this secondary thread to finish
(DocThread.Join). I presume this is the problem.
I tried removing the Invoke calls and sure enough the program no longer hung
but of course I no longer added the nodes to the TreeView so defeating the
purpose of the thread.

If my explaination is correct then how can I get around this problem. I need
to be able to cancel the thread and I also need to be able to wait for the
thread to finish normally. Obviously I can't use both the Invoke method (add
nodes to the TreeView) and run Join or use the WaitOne method of
ManualResetEvent.

Any help here would be appreciated.


Private Sub CancelUpdateAllNodeFonts()
If Not (DocThread Is Nothing) Then
If DocThread.IsAlive Then
CancelDocThread.Set() ' Set CancelThread to ask the thread to
stop.
DocThread.Join()
End If
End If
End Sub
 
Fred,
My thread adds nodes to a TreeView but to do this from another thread I had
to use the Invoke method. Am I correct in thinking that the Invoke method
then tries to run the delegate method in the main thread? If so what happens
if the main thread is waiting for this secondary thread to finish
(DocThread.Join). I presume this is the problem.
You are correct, Control.Invoke is trying to run something on the main
thread, the main thread is waiting on the secondary thread.

Welcome to multi-threaded programming. You have entered the state known as
Dead-lock!


What you could do is add the timeout parameter to the Join method, allowing
the main thread to "come up for air". If Join returned false, call DoEvents,
look back, so the Control.Invoke is allowed to finish... At least I believe
DoEvents will be required, as I think that is how Control.Invoke gets the
"delegate" to the main thread (via a Win32 message).

Something like:
Private Sub CancelUpdateAllNodeFonts()
If Not (DocThread Is Nothing) Then
If DocThread.IsAlive Then
CancelDocThread.Set() ' Set CancelThread to ask the thread to
stop.
Do Until DocThread.Join(500) ' try for half a second
Application.DoEvents()
Loop
End If
End If
End Sub

I would probably limit the number of times I tried to Join the DocThread.

Hope this helps
Jay
 
Back
Top