473,583 Members | 3,556 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

locking question when using generic dictionary

1 New Member

I have a class MyEmbededList contains a generic dictionary, the value field is actually the MyEmbededList type as well.
There is another class need to access and manipulate a list of MyEmbededList (please refer to the MyTestClass below).

I am not sure whether I implements the right locking mechanism here and hope someone can give me some advices.

I have provided some codes for these two classes below. My questions are:
1. Am I doing the right locking in the TestMehod in the TestClass below? I try to avoid locking the m_topList too long so try to lock the child list.After obtain the embededList that I need to modify then just lock that embededList. However, I am not sure whether whether the implementation of the locking is correct or not...

2. In such scenario, the locking is implemented in the TestClass, I also have locking in some method in MyEmbededList, for example, the AddNextLevelLis tItem method. If the TestClass will call the AddNextLevelLis tItem, should the locking be done by the TestClass and remove the locking in the MyEmbededList? or should I keep the locking in both? any drawbacks?

Thank you very much

public class MyEmbededList
private string m_name;
private Dictionary<stri ng, MyEmbededList> m_nextLevelList ;
priviate bool dummyStatus;

public Dictionary<stri ng, MyEmbededList> NextLevelList
return m_nextLevelList ;
m_nextLevelList = value;

public MyEmbededList(s tring _name)
m_name = _name;
m_nextLevelList = new Dictionary<stri ng, MyEmbededList>( );

public MyEmbededList GetNextLevelLis tItem(string _key)
MyEmbededList subList = null;

lock (((IDictionary) m_nextLevelList ).SyncRoot)
m_nextLevelList .TryGetValue(_k ey, out subList );
return subList ;

public void AddNextLevelLis tItem(string _key, MyEmbededList _emList)
lock (((IDictionary) m_nextLevelList ).SyncRoot)
if (!m_nextLevelLi st .ContainsKey(_k ey))
m_nextLevelList [_key] = _emList;


public static class MyTestClass
private static Dictionary<stri ng, MyEmbededList > m_topList = new Dictionary<stri ng,MyEmbededLis t >();

public static void TestMehod(strin g[] _keys)
MyEmbededList letterDic = null;
Dictionary<stri ng, MyEmbededList > dic = m_topList;

// the goal is to get the deppest possible MyEmbededList and update its
// for example, if _keys = {"1", "2", "5"}
// we get the MyEmbededList (e.g. list1) from m_topList["1"]
// then get the next level MyEmbededList (e.g. list 2) from list1.m_nextLev elList["2"]
// then get the next level MyEmbededList (e.g. list 3) from list2.m_nextLev elList["5"]
// so list 3 is the list that I want to update the properties (e.g. dummyStatus)
// if list 3 not found, then return the previous found list (e.g. list 2) and update the
// properties of list 2 instead

for (int i = 0; i < _keys.Length; i++)
string key= _keysIdea;
lock (((IDictionary) dic).SyncRoot)
if (dic.ContainsKe y(key))
letterDic = dic[key];
dic = letterDic.NextL evelList;
} // end for loop

lock (letterDic)
// update some properties for this object
Mar 23 '07 #1
0 1314

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Edward Diener | last post by:
Version 2.0 of the Python database API was written over 5 years ago, in 1999. While it has been used successfully by many implementations, there is no generic access into the data dictionary of relational databases except at the table column level. I am working on some Python which would hopefully give me a form of generic access to more...
by: Eyvind W Axelsen | last post by:
When I open an existing component containing an OleDbDataAdapter, or when I try to add an OleDbDataAdapter to a component, I receive the following error message. Up til today, this has worked flawlessly. I am using C# in VS.NET 2005 beta 2. Error HRESULT E_FAIL has been returned from a call to a COM component. at...
by: Elhanan | last post by:
hi.. i wanted to build a Dictionary Classs that will load my own class called letter, i understood that i implement the IEquatable interface's equles method that then the dictionary would use that inorder to compare the keys, so i went ahead the implented it in the LETTER class so letter would provice it's own means for comparison, but the...
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of 'Delegate as a valid Type declaration for the Value of a Generic Dictionary. I am curious as to why I can compile and use this syntax : it seems to...
by: =?Utf-8?B?YWluZHJlaQ==?= | last post by:
This code uses lazy initialization and double checked locking to read urls associated with a retailer from a map. Will the code work? I'm not sure how volatile applies to references. private static volatile Dictionary<string, stringurlMap_; private static object syncRoot = new Object(); public static string GetUrl(string retailerId) {
by: Akula | last post by:
Does anyone know whether or not it is faster to lock a simple object, rather than a complex type? For example: Dictionary<string, SomeOtherClassdict = new Dictionary<string, SomeOtherClass>(); object lockObject = new object(); --- Example 1 ---
by: =?Utf-8?B?RGF2aWQgTW9ycmlz?= | last post by:
I am trying to create a nested Dictionary and get an error that seems odd to me. Here is my declaration: private IDictionary<Guid, IDictionary<Guid, string>> myNestedDictionary = new Dictionary<Guid, Dictionary<Guid, string>>(); I get an error unless I nest a Dictionary and not an IDictionary: Error 3 Cannot implicitly convert type
by: eric | last post by:
I'm trying to define a dictionary whose value is an Generic Action<> delegate private Dictionary<string, List<Action<T>>> Any ideas on to how specify T or a different collection type more suitable to this? Thanks
by: polastine | last post by:
Hi Anthony -- Anthony Jones wrote: OK, figured as much. I'm not forking any threads myself. 2.0, currently. Why? Is there a difference/advantage? I can probably go to 3.x if there's a good reason for it.
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...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
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...
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...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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...

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.