Avatar of nicksbell
nicksbell
 asked on

Using SyncLock to control access to a hastable

Hi, I have the following code

Dim htb as hashtable = htbTrades.Clone
htbTrades.Clear

Open in new window


In this example htbTrades is a hashtable that is declared as a global variable and can be added to by other threads.  I want to make sure that no items can be added to the htbTrades hashtable between the Clone and Clear operations.  Is SyncLock what I should be using.  Will the following achieve my objective.

Dim htb as hashtable
SyncLock htbTrades.SyncRoot
        htb = htbTrades.Clone
        htbTrades.Clear()
End SyncLock

Open in new window


Thanks
.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
nicksbell

8/22/2022 - Mon
lojk

http://msdn.microsoft.com/en-us/library/system.collections.hashtable.clone.aspx

Well - yes and no. It will only shallow copy the hashtable so a lot depends on what you are containing in the hashtable. Heres the best way i can explain that...

Imagine you have 10 cups on a table and each cup is connected to a Keyring via a peice of string. Cloning the hashtable merely adds another keyring and another 10 peices of string connected to the original cups.  If you want 10 more cups, then i think you may need to CopyTo instead.

If you are only storing value types (and/or strings) it should work fine but if you are using it to hold instances of complex types/classes you might need to check what you are trying to acheive is actually working correctly.
ASKER CERTIFIED SOLUTION
Mike Tomlinson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
lojk

I'm with idlemind - in fact i wouldn't even expose it is a global variable at all rather a private one with a readonly property and a few public methods to proxy access to it (AddToHash, CopyHash etc) with the synclocks in place in there.
nicksbell

ASKER
Thanks for your replies.

lokj, re my first comment, my issue is not with the Clone method.  I know that works for what I am trying to do as I already have that in production.  I simply want to make sure that no items can be added to the hashtable after Clone and before Clear, as then they would be lost altogether.  I am not worried about the underlying objects contained in the hashtable as they are static and are never updated, only new ones are added.

Idle_mind, ok, I didn't realise that but that makes sense.

lokj, just to clarify, are you suggesting I create a class that inherits the hashtable class and then expose a public AddToHash method (which would handle the synclock) and all other code would call AddToHash?  I guess that makes a lot of sense too as it stops the need for synclocks all over the place.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
lojk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
nicksbell

ASKER
Thanks, very useful