Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing the Concurrent Dictionary - Thread Safe Collection Modification
    text
    copied!<p>Recently I was running into the following exception when using a generic dictionary</p> <blockquote> <p>An InvalidOperationException has occurred. A collection was modified</p> </blockquote> <p>I realized that this error was primarily because of thread safety issues on the static dictionary I was using.</p> <p>A little background: I currently have an application which has 3 different methods that are related to this issue.</p> <ol> <li>Method A iterates through the dictionary using <code>foreach</code> and returns a value.</li> <li>Method B adds data to the dictionary.</li> <li>Method C changes the value of the key in the dictionary.</li> </ol> <p>Sometimes while iterating through the dictionary, data is also being added, which is the cause of this issue. I keep getting this exception in the <code>foreach</code> part of my code where I iterate over the contents of the dictionary. In order to resolve this issue, I replaced the generic dictionary with the <code>ConcurrentDictionary</code> and here are the details of what I did.</p> <p>Aim : My main objective is to completely remove the exception</p> <p><strong>For method B</strong> (which adds a new key to the dictionary) I replaced <code>.Add</code> with <code>TryAdd</code></p> <p><strong>For method C</strong> (which updates the value of the dictionary) I did not make any changes. A rough sketch of the code is as follows :</p> <pre><code> static public int ChangeContent(int para) { foreach (KeyValuePair&lt;string, CustObject&gt; pair in static_container) { if (pair.Value.propA != para ) //Pending cancel { pair.Value.data_id = prim_id; //I am updating the content return 0; } } return -2; } </code></pre> <p><strong>For method A</strong> - I am simply iterating over the dictionary and this is where the running code stops (in debug mode) and Visual Studio informs me that this is where the error occured.The code I am using is similar to the following</p> <pre><code> static public CustObject RetrieveOrderDetails(int para) { foreach (KeyValuePair&lt;string, CustObject&gt; pair in static_container) { if (pair.Value.cust_id.Equals(symbol)) { if (pair.Value.OrderStatus != para) { return pair.Value; //Found } } } return null; //Not found } </code></pre> <p>Are these changes going to resolve the exception that I am getting. </p> <p><strong>Edit:</strong></p> <p>It states on <a href="http://arbel.net/2013/02/03/best-practices-for-using-concurrentdictionary/" rel="nofollow">this page</a> that the method <code>GetEnumerator</code> allows you to traverse through the elements in parallel with writes (although it may be outdated). Isnt that the same as using foreach ? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload