I’m looping through a List of elements with foreach, like this:
foreach (Type name in aList) {
name.doSomething();
}
However, in an another thread I am calling something like
aList.Remove(Element);
During runtime, this causes an InvalidOperationException: Collection was modified; enumeration operation may not execute. What is the best way to handle this (I would perfer it to be rather simple even at the cost of performance)?
Thanks!
Thread A:
Thread B:
This ofcourse is really bad for performance.