I have a dictionary where if the item in the dictionary passes through all the processing I want to remove it.
var dictEnum = dictObj.GetEnumerator();
while (dictEnum.MoveNext())
{
Parallel.ForEach(dictObj, pOpt, (KVP, loopState) =>
{
processAndRemove(KVP.Key);
});
}
private void processAndRemove(string keyId)
{
try
{
<does stuff>
dictObj.Remove(keyId);
} catch(exception ex) {
...
<does not remove anything, wants to retry until it doesn't fail>
}
}
I'd like for the loop to continue processing with all the remaining items (non-removed) in the dictionary.
However, I'm getting an error. When I run a simpler version of this code I get a message stating:
Collection was modified; enumeration operation may not execute
Is there a way to do this using a dictionary?
Update:
Just to give more context. The idea behind this is so the loop continues to run if there are items left in the dictObj. So if I start with 10 and 8 pass I want to re-run the 2 that didn't pass until they do.