Use LockRecursionPolicy.SupportsRecursion when creating an RWLS instance. If the error goes away, you really have some sort of recursion. Perhaps this is code that you did not publish?
And if you are really worried that you will get the most concurrency from this (as I suspect you have been using RWLS ever since), you can use a double-check lock pattern. Note that your source code already has this feeling? So why beat around the bush? Just do it.
In the following code, note that I always consider the Breeds link to be immutable, and then inside the lock I double-check, copy, modify, and replace the link.
static volatile Dictionary<string, BreedOfDog> Breeds = new Dictionary<string,BreedOfDog>(); static readonly object LockObject = new object(); static BreedOfDog GetBreed(string name) { BreedOfDog bd; if (!Breeds.TryGetValue(name, out bd)) { lock (LockObject) { if (!Breeds.TryGetValue(name, out bd)) { bd = new BreedOfDog(name); var copy = new Dictionary<string, BreedOfDog>(Breeds); copy[name] = bd; Breeds = copy; } } } return bd; }
Brian gideon
source share