473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Thread Abort and SyncLock

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
Nov 20 '05 #1
4 2528
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

Nov 20 '05 #2
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


Nov 20 '05 #3
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

"fred" <So***@NoSpam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... 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



Nov 20 '05 #4
Thanks for your help Jay.

Regards,
Fred
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: Charles Law | last post by:
Hi guys. I'm back on the threading gig again. It's the age-old question about waiting for something to happen without wasting time doing it. Take two threads: the main thread and a worker...
4
by: Daylor | last post by:
hi. i have multi thread application in vb.net is there a way NET support, so i can mark the class , to be access only for 1 thread each time ? if there is , small sytax sample will help ...
9
by: Li Pang | last post by:
Hi I make an app which can run some sub processes through multiple threads. I'd like to know how to terminate all sub-threads when the main thread is closed thanks in advance
7
by: Kejpa | last post by:
Hi, I'm logging the values my app is producing, in order to keep the logs small I zip them hourly. My problem lies in that two (or more) different objects discover that the hour has changed and...
2
by: shipcreak | last post by:
I have an interesting problem with a sort of producer-consumer system for error logging. Consider the following code: <code> SyncLock _eventList.SyncRoot Dim item As ExceptionLogEntry '...
3
by: daan | last post by:
Hello, I have a problem and I can't get the solution for it :( I have a com dll, which i imported as a reference. The com object is part of a class which is multithreaded and will create...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
4
by: fniles | last post by:
I create a thread where I pass thru a message. When I click very fast many times (like 50 times) to create 50 threads, the message did not get pass thru ProcessMessage. For example: strBuffer =...
5
by: P.J.M. Beker | last post by:
Hi there, I'm currently writing a program in which I use the FileMonitor to monitor a folder in which I store downloaded images. I know that I can't add much coding in the filemonitor's event in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.