Lock on Dictionary's TryGetValue() - Performance issues

What you're trying to do here is simply not a supported scenario. The TryGetValue occurs outside of the lock which means is very possible for one thread to be writing to the dictionary while others are simultaneously calling TryGetValue. The only threading scenario inherently supported by Dictionary<TKey, TValue> is reads from multiple threads. Once you start reading and writing from multiple threads all bets are off.

In order to make this safe you should do one of the following

  • Use a single lock for all read or write accesses to the Dictionary
  • Use a type like ConcurrentDictionary<TKey, TValue> which is designed for multi-threaded scenarios.

Either the usage of that collection by your code is thread-safe, in which case you don't need the lock, or it's not thread-safe, in which case you ALWAYS need the lock.

Try this using ConcurrentDictionary instead, which is thread-safe.


Dictionary isn't thread-safe. If anything's adding to the dictionary when you do a TryGetValue, things can go badly. Your first call to TryGetValue is not protected by a lock. So if thread A is doing an Add and thread B enters that first TryGetValue, it can throw an exception.

Consider using System.Collections.Concurrent.ConcurrentDictionary. Or be sure to lock the dictionary on every access. Probably using ReaderWriterLockSlim.