c# - .NET Dictionary: Potential concurrency problems? -


I am working on the maintenance of a .NET project, and I'm having some problems with which I happily share If you (= evil item number & lt; 1) {foreach (GetAnotherDict (item items in item) if (evil!)

(Item.name.ToLower (.) Trim ())) evilDict.add (despite item.name.ToLower () trim (), item.ID);}

() - Inquiry, I'm getting an argument that an item with a key has already been added. Only this problem has been achieved in production, never in trial, which makes me doubt the problem of a concurrency. I am wondering:

  • Do you think it is a What is the problem of concurrency?
  • How do I fix it?
  • Is my fixed viable (see below)?
  • This is my first rip on the net, Dictionary is usually the source of problems?

the place of the dictionary, my place)

  Protected Static Zero DictAddHelper (Dictionary & lt; String, int & gt; Dictionary, string key, int value) {lock (dict) {key = key.ToLower (). If (dict.ContainsKey (key) == incorrect) {try {dict.Add (key, value); } Hold (argument expression AX) {stringbinder debuginfo = new stringbuilder (); DebugInfo.AppendLine ("An argument expression occurred:" + aex.Message); DebugInfo.AppendLine ("key =" + key); DebugInfo.AppendLine ("value =" + value); DebugInfo.AppendLine ("--- is in the dictionary ---"); Foreign (dict.Keys in string k) debugInfo.AppendLine (k + "=" + dict [k]); Logs. Error (debuginfo, ax); }}}}  

edit:

Do not require those suggestions that require the threading and safe implementation of the Dick Class I'm because it will be a huge refactoring which will not be very welcome =)

EDIT2:

I tried

  lock (((literally) dict) .syncroot)  

but I get

  error 28 General Type Use 'System Collections. Generic.IDictionary & LT; TKey, TValue & gt; ' 

After this I try:

  lock ((id works ) dict). SyncRoot)  

Error:

  Error 28 'System.Collections.Generic.IDictionary & lt; String, int & gt; 

There is no definition for the last edit (I think):

Thanks for all the answers!

Now, what I want to know is this. Does my method (Dictadhelper) work at all, and if not, then why?

If you suspect that you have concurrent problems accessing the dictionary, your Fixes will not be of any use It will solve the specific problem you experienced, however, if you have concurrent access to the dictionary having more problems in the future.

When you are modifying it, consider locking the dictionary's access.


Comments