I've read a few links here but i couldn't come up with an answers to my question.
What i'm trying to achieve is to add the results of the Parallel.ForEach into a ConcurrentDictionary. However, how can i be sure that i'm adding the result of the iteration instead of a null value ?
I mean: I want to add the variable after the operation is done. I'm worried that i may add a null value (i.e: add to the collection an uncomplete iteration).
My code goes below. Thanks for any tip.
I've also read about using lock being faster than the ConcurrentDictionary but i think that on my case it won't make an significant difference.
Thanks in advance.
public Dictionary<string,IMagickImage> ComposedImages { get; private set; }
public ParallelImageComposer(Dictionary<string,MagickImage> InputImages, MagickImage InkZoneImage, int OrientationNumber)
{
var resultCollection = new ConcurrentDictionary<string, IMagickImage>();
Parallel.ForEach(InputImages, CurrentKeyValuePair =>
{
var img = new ImageComposer(InkZoneImage, CurrentKeyValuePair.Value, OrientationNumber).ComposedImage;
resultCollection.TryAdd(CurrentKeyValuePair.Key, img);
});
ComposedImages = resultCollection.ToDictionary(x => x.Key, x => x.Value);