472 Views
ConcurrentDictionary.GetOrAdd may cause unwanted side effects where the code that produces the value may be run more than once. This is a discussion of that scenario, along with a TDD-style walk-through of a solution.
3 comments
dpeterson
1/11/2012 9:33:39 AM
Very cool stuff. Alternatively, one could possibly push the value updating to the task, but this is cooler ;-)
fredrikmork
1/11/2012 1:06:14 PM
Thanks! In the concrete case that I had it was necessary to prevent the task to get started unless it was added to the dictionary (it performed a rather slow operation on a networked device). Or perhaps I misunderstand what you mean?
dpeterson
1/11/2012 2:01:54 PM
No you understood me correctly. You're right though, in your case it wouldn't have made sense to run the task as it involved network IO. I was just speaking in the general sense. :-)