<p>I'm looking for a way of getting a <strong>concurrent collection</strong> in <strong>C#</strong> or at least a collection which supports a <strong>concurrent enumerator</strong>.  Right now I'm getting an <code>InvalidOperationException</code> when the collection over which I'm iterating changes.  </p>

<p>I could just deep copy the collection and work with a private copy but I'm wondering  if there is perhaps a better way</p>

<p>Code snippet:</p>

<pre class="lang-cs prettyprint-override"><code>foreach (String s in (List&lt;String&gt;) callingForm.Invoke(callingForm.delegateGetKillStrings))
{
    //do some jazz
}
</code></pre>

<p>--edit--</p>

<p>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.</p>

<pre class="lang-cs prettyprint-override"><code>private void addKillString(String s)
{
    lock (killStrings)
    {
        killStrings.Add(s);
    }
}
</code></pre>

[{'answer_id': 38765, 'author': 'Damian', 'author_id': 3390, 'author_profile': 'https://Stackoverflow.com/users/3390', 'pm_score': 4, 'selected': True, 'text': '<p>Other than doing a deep-copy your best bet might be to lock the collection:</p>\n\n<pre><code>   List&lt;string&gt; theList = (List&lt;String&gt; )callingForm.Invoke(callingForm.delegateGetKillStrings);\n    lock(theList.SyncRoot) {\n        foreach(string s in theList) {\n               // Do some Jazz\n        }\n    }\n</code></pre>\n'}, {'answer_id': 38768, 'author': 'Domenic', 'author_id': 3191, 'author_profile': 'https://Stackoverflow.com/users/3191', 'pm_score': 2, 'selected': False, 'text': '<p>So I\'m not quite sure what you\'re asking, but the Parallel Extensions team has put together some stuff that might fit the bill. See this blog post in particular, about <a href="http://blogs.msdn.com/pfxteam/archive/2008/08/12/8852005.aspx" rel="nofollow noreferrer">enumerating parallel collections</a>. It also contains a link to download the Parallel CTP, and you can of course browse through the rest of the blog posts to get an idea of what the CTP is meant to do and how the programming model works.</p>\n'}, {'answer_id': 39666, 'author': 'McKenzieG1', 'author_id': 3776, 'author_profile': 'https://Stackoverflow.com/users/3776', 'pm_score': 1, 'selected': False, 'text': '<p>If you want to use the FCL collections, then locking is the only way to support iteration / modification from multiple threads that may overlap.</p>\n\n<p>Be careful what you use as your lock object, though. Using SyncRoot is only a good idea if the collection itself is a private member of the class that uses it. If the collection is protected or public, then a client of your class can take its own lock on your SyncRoot, potentially deadlocking with code in your class. </p>\n\n<p>If you are interested in taking a look at a 3rd-party collection library, I recommend the excellent <a href="http://www.itu.dk/research/c5/" rel="nofollow noreferrer">C5 Generic Collection Library</a>. They have a family of tree-based collections that can easily and safely be modified and iterated at the same time without locking - see sections 8.10 and 9.11 of their (excellent) documentation for details.</p>\n'}]
['https://Stackoverflow.com/questions/38756', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/361/']