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
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eA**************@tk2msftngp13.phx.gbl...
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
"fred" <So***@NoSpam.com> wrote in message
news:ev**************@TK2MSFTNGP09.phx.gbl... 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