473,324 Members | 2,179 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,324 software developers and data experts.

Thread Safe question when using a Dictionary

I'm new to .Net and I inherited a Web App from some guys who left the company. The App stores some stuff from the Database in a Dictionary so that the first time the data is accessed it is slow but the next time it is fast.

The App locks the Dictionary whenever it puts something from the database into the dictionary - and then it returns it to the caller. But when the data is already in the dictionary it just returns the data without locking the dictionary. Is this ok or should it lock the dictionary before it returns the data? Here's the code:

//"Name" is passed in
//The Dictionary is called MetaDataList

//Should there be a lock here?
if (!MetaDataList.ContainsKey(Name))
return MetaDataList[Name];

lock (MetaDataList)
{
if (!MetaDataList.ContainsKey(Name))
{
MetaData mt = new MetaData(md.Rows[0]); //Get the Database stuff
MetaDataList.Add(Name, mt); //Put it in the Dictionary for the next time
}
}
return MetaDataList[Name];
Dec 11 '06 #1
0 1412

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

Similar topics

9
by: Mats Olsson | last post by:
I have an xml file, which is to be updated by an asp script (vbs) when users are submitting forms from their browsers. Now I wonder if there is anything that stops the following to occur: 1....
5
by: nicolas_riesch | last post by:
Does someone know if the module pytz (http://sourceforge.net/projects/pytz/) is thread-safe ? I have not seen it explicitely stated, and just wanted to be sure, as I want to use it. That's...
0
by: Dante | last post by:
I have moved a heavy calculation inside my application to multiple threads. Previously I was saving the results to a file and after finishing I collected them all in one mdb file. Unfortunately...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
3
by: BoloBaby | last post by:
All, I believe I am having a threading problem. Class "BELights" is part of a larger DLL that is used by my main application. A user control (of type BESeat) within the main application raises...
1
by: Toon Verstraelen | last post by:
Hi, I recently had a thread problem and I could reduce it to a very short example that shows the problem. I hope it has its origin in my misunderstanding of how python threads work. Here it is:...
6
by: blackstreetcat | last post by:
consider this code : int i; //gobal var Thread1: i=some value; Thread2: if (i==2) dosomething(); else dosomethingelse();
2
by: tamara.roberson | last post by:
I am porting some code to use generics. Currently, we have a synchronized Hashtable called as follows: private Hashtable table = Hashtable.Synchronized (new Hashtable ()); What I would like...
12
by: David | last post by:
Below are three classes for a console application. If put into three separate files, the sub main() will launch multiple threads adding and removing the same value. At the end we expect the value...
1
by: jecheney | last post by:
Hi, Im currently using the following code for reading/writing to a network socket. private StreamReader clientStreamReader; private StreamWriter clientStreamWriter; .... TcpClient tcpClient...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.