Throw a NullReferenceException while calling the set_item method of a Dictionary object in a multi-threading scenario Throw a NullReferenceException while calling the set_item method of a Dictionary object in a multi-threading scenario multithreading multithreading

Throw a NullReferenceException while calling the set_item method of a Dictionary object in a multi-threading scenario


As the exception occurs internally in the Dictionary code, it means that you are accessing the same Dictionary instance from more than one thread at the same time.

You need to synchronise the code in the GetInstance method so that only one thread at a time accesses the Dictionary.

Edit:
Lock around the accesses separately, so that you aren't inside a lock while doing the (supposedly) time consuming loading:

private static object _sync = new object();public static object GetInstance(string key) {   object instance = null;   bool found;   lock (_sync) {      found = Instances.TryGetValue(key, out instance);   }   if (!found) {      instance = LoadInstance(key);      lock (_sync) {         object current;         if (Instances.TryGetValue(key, out current)) {            // some other thread already loaded the object, so we use that instead            instance = current;         } else {            Instances[key] = instance;         }      }   }   return instance;}


As of .Net 4 you have ConcurrentDictionary which is a thread-safe dictionary, no more need for "manual" synchronization.


To quote http://msdn.microsoft.com/en-us/library/xfhwa508.aspx (emphasis added by me):

"Thread Safety

Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

A Dictionary<(Of <(TKey, TValue>)>) can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with write accesses, the collection must be locked during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization."