I have a Parallel.ForEach loop doing some cure. But the first operation is to add a value to the dictionary if the key is not contained. I get an error when adding it, it says that the key is already in the dictionary. I assume that the key was added by the parallel process after checking the .Contains of this stream, but before adding it. Besides putting this line in try-catch, is there another simple solution I can use to prevent this error?
Parallel.ForEach(branchFixes, b => { Parallel.ForEach(b.Value, t => { var team = t.Key; if (!resultTeamDict.ContainsKey(team)) { resultTeamDict.Add(team, new Dictionary<FixItem, Dictionary<BranchInfo, bool>>()); } }); });
dictionary c # parallel.foreach
Amaranth
source share