473,756 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2558
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 ThreadAbortExce ption, 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
ManualResetEven t.

Hope this helps
Jay

"fred" <So***@NoSpam.c om> wrote in message
news:ev******** ******@TK2MSFTN GP09.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
ManualResetEven t.
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
ManualResetEven t.

Any help here would be appreciated.
Private Sub CancelUpdateAll NodeFonts()
If Not (DocThread Is Nothing) Then
If DocThread.IsAli ve 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******** ******@tk2msftn gp13.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 ThreadAbortExce ption, 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
ManualResetEven t.

Hope this helps
Jay

"fred" <So***@NoSpam.c om> wrote in message
news:ev******** ******@TK2MSFTN GP09.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 CancelUpdateAll NodeFonts()
If Not (DocThread Is Nothing) Then
If DocThread.IsAli ve Then
CancelDocThread .Set() ' Set CancelThread to ask the thread to
stop. Do Until DocThread.Join( 500) ' try for half a second
Application.DoE vents()
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.c om> wrote in message
news:%2******** ********@TK2MSF TNGP09.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
ManualResetEven t.
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
ManualResetEven t.

Any help here would be appreciated.
Private Sub CancelUpdateAll NodeFonts()
If Not (DocThread Is Nothing) Then
If DocThread.IsAli ve 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******** ******@tk2msftn gp13.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 ThreadAbortExce ption, 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
ManualResetEven t.

Hope this helps
Jay

"fred" <So***@NoSpam.c om> wrote in message
news:ev******** ******@TK2MSFTN GP09.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
2370
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 thread. The worker thread is reading the serial port, waiting for something to happen (a service request). When it does it raises an event. Of course, the event is executed on the worker thread. The idea is that when the event is raised, the handler...
4
1557
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 //what i need to add , so only 1 thread per time can access this class in MultiThread app.
9
7421
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
1167
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 each tries to start a thread to create the hourly zip. The runner up discovers that there is no file and creates it, but during this time the first thread already has created the file and I get an "File already exists"-error. Basically, I want...
2
2131
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 ' If there are items, get the top one If _eventList.Count > 0 Then
3
2285
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 seperate objects which we can and must control. On these com objects I added the events via AddHandler. This is working great, I can see that my threads are raising events through the com object.
6
3140
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 to the client. Before adding data to the arraylist, I check if the depth of the arraylist is longer than iMaxQueueDepth, and if it is, I clear the arraylist. Is it possible that while I am clearing the arraylist, the ThreadMain at the same time...
4
1420
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 = "#TRADE, D1410-123456, BUY, 1, ESM7, DAY, LIMIT, 1490.00, , , 0, 0, 0, 0, 0, links |52994/25/2007 10:47:17 AM !A", when I trigger to create many threads (like 50), this message did not get to sub ProcessMessage in clsEachMessage. I have added...
5
1792
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 risk of losing some new entries, so I've deceided to create an update thread. This thread is created when the program's start and should (for various reason) run not in sync with the Filemonitor. The Filemonitor event creates an entry in a...
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.