I’m looking for a way of getting a concurrent collection in C# or at least a collection which supports a concurrent enumerator. Right now I’m getting an InvalidOperationException when the collection over which I’m iterating changes.
I could just deep copy the collection and work with a private copy but I’m wondering if there is perhaps a better way
Code snippet:
foreach (String s in (List<String>) callingForm.Invoke(callingForm.delegateGetKillStrings)) { //do some jazz }
–edit–
I took the answer but also found that I needed to ensure that the code which was writing to the collection needed to attempt to get a lock as well.
private void addKillString(String s) { lock (killStrings) { killStrings.Add(s); } }
Other than doing a deep-copy your best bet might be to lock the collection: