473,396 Members | 1,893 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,396 software developers and data experts.

Question about Synclock and exceptions

I was using a Queue object like this to create my own specialized queue
class for use with my own objects:

Public Class MySpecializedQueue

Private q As New Queue

Public Sub Enqueue(obj As MyCustomClass)
SyncLock q.SyncRoot
q.Enqueue(obj)
End SyncLock
End Sub

Public Function Dequeue() As MyCustomClass
SyncLock q.SyncRoot
Return DirectCast(q.Dequeue, MyCustomClass)
End SyncLock
End Function

End Class

What happens if there is an exception inside the SyncLock block for
some reason? Will the q now be locked to other threads? Or does the
End Synclock automatically happen?

Chris

Nov 21 '05 #1
3 4769
Chris,
What happens if there is an exception inside the SyncLock block for
some reason? Will the q now be locked to other threads? Or does the
End Synclock automatically happen? Yes.

SyncLock is basically a Try/Finally block with calls to Monitor.Enter &
Monitor.Exit.

So SyncLock is effectively Monitor.Enter & a Try, while End SyncLock is
effectively Finally with Monitor.Exit...

You can use ILDASM.EXE to see the actual IL generated.

Hope this helps
Jay
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...I was using a Queue object like this to create my own specialized queue
class for use with my own objects:

Public Class MySpecializedQueue

Private q As New Queue

Public Sub Enqueue(obj As MyCustomClass)
SyncLock q.SyncRoot
q.Enqueue(obj)
End SyncLock
End Sub

Public Function Dequeue() As MyCustomClass
SyncLock q.SyncRoot
Return DirectCast(q.Dequeue, MyCustomClass)
End SyncLock
End Function

End Class

What happens if there is an exception inside the SyncLock block for
some reason? Will the q now be locked to other threads? Or does the
End Synclock automatically happen?

Chris

Nov 21 '05 #2
Thanks Jay,

Is there any way to check to see if an object is currently locked? If
a thread has entered a syncLock, is there a way to find out which
thread has the lock?

Just wondering

Nov 21 '05 #3
Chris,
Is there any way to check to see if an object is currently locked? You can use Monitor.TryEnter to see if an object is currently locked.
However it will give you the lock if it is currently not owned.
If
a thread has entered a syncLock, is there a way to find out which
thread has the lock? Non that I know of. With effort you could probably create your own class
that supports knowing who has the lock... However even then you may have
race conditions or performance conditions...

Hope this helps
Jay
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com... Thanks Jay,

Is there any way to check to see if an object is currently locked? If
a thread has entered a syncLock, is there a way to find out which
thread has the lock?

Just wondering

Nov 21 '05 #4

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

Similar topics

3
by: kelkel | last post by:
Hi all, I got a question here which about using queue. i have create a class of queue. Public Sub Add(ByVal data As Object) SyncLock Me _Data.Enqueue(data) Monitor.Pulse(Me) End SyncLock End...
3
by: Bob Day | last post by:
Ok, I have done a lot of reading(of the newsgroup answers, help files and MSDN articles) of synclock. I understand what you are saying in the newsgroup, and it is very helpful. It does, however,...
4
by: Jeff Stewart | last post by:
Specifically, I don't understand the parameter that Synclock accepts. How is a reference type a lockable entity? What -is- a reference type? Is it a number? Is it a value at a specific memory...
7
by: SD | last post by:
I have a public object that I only want one thread to access at a time. However the access to this object is not limited to one procedure. Will SyncLock work in this case? If not what options do I...
1
by: fred | last post by:
I have a VB application that is using MS Access as its database. To avoid connection delays the application creates one connection to the database at start-up and maintains that single connection...
4
by: Justin | last post by:
I am having trouble figuring out the best what to accomplish this fairly simple goal. I have a program that controls a 1-wire network and I can only make requests to it one at a time, but I will...
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...
1
by: buu | last post by:
If I have a code like: TRY SYNCLOCK Object1 //some code END SYNCLOCK CATCH END TRY
6
by: fniles | last post by:
I am using VB.NET 2005 and thread. In each thread I open a database connection and before it exits the thread I close the database connection. Is there any way for me to count how many threads are...
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
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
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.