none
Collection Modified Exception During Multi-Thread Session RRS feed

  • Question


  • I'm developing a code in C# and I've got a collection modified Exception in a foreach loop which i couldn't find the solution due to 2 reasons:
    - im using a Dictionary and i can't use for instead. (i dont know how to do it... could someone tell me?)
    - the dictionary is modified(im adding itens to the dictionary) by another thread that isn't the one in which the exceptions occurs

    let me post part of the code:

       while (true)
    
                {
    
                    if (callTimes.Count > 0)
    
                    {
    
                        Dictionary<string, Call> callsToDelete = new Dictionary<string, Call>();
    
                        
    
                            foreach (string key in calls.Keys)
    
                                      
    
    
    
                            {
    
                                                      
    
                                foreach (string callID in pctsdps.Keys)
    
                                    
    
                                {/* some code here*/ }
    
                                    {
    
                                        callsToDelete.Add(key, calls[key]);
    
                                    }
    
                                }
    
                            }
    
    
    i get exceptions in both the foreach loops

    here is the part of the other thread in which the dictionaries are modified


    private void AddSDP(string key, SDPInfo sdpInfo)
    
            {
    
                lock (pctsdps)
    
                {
    
    
    
                    if (!pctsdps.ContainsKey(key))
    
                    {
    
                        pctsdps.Add(key, new SDPInfo[3]);
    
                        
    
                    }
    
                }
    
            }

    private void AddFrame(string key, RTPInfo rtpInfo)
    
            {
    
                
    
                if (calls.ContainsKey(key))
    
                {
    
                    lock (calls)
    
                    {
    
                        calls[key].Add(rtpInfo);
    
                    }
    
                    lock (callTimes)
    
                    {
    
                        callTimes[key] = DateTime.Now;
    
                    }
    
    
    
                    return;
    
                
    
                }
    
    
                Call call = new Call();
    
                foreach (string key in pctsdps.Keys)
    
                {/*some code*/ }

    lock (calls) { call.Add(rtpInfo); calls.Add(key, call); callTimes.Add(key, DateTime.Now); }
    Wednesday, June 3, 2009 6:21 PM

Answers

All replies

  • You are trying to modify the enumeration while the we are enumerating.
    Try removing the add statement inside the foreach loop.



    Thanks,
    A.m.a.L
    .Net Goodies
    Remember to click "mark as answered" when you get a correct reply to your question
    Wednesday, June 3, 2009 7:12 PM
  • that's not the problem... the add reffers to another dictionary

    but thx anyway
    Wednesday, June 3, 2009 7:33 PM
  • The problem is:

    "the dictionary is modified(im adding itens to the dictionary) by another thread that isn't the one in which the exceptions occurs"

    You cannot modify a dictionary while enumerating through it - it breaks the enumerator.

    Normally, the way around this is to use locking (on both add and query) to prevent the other thread from adding during your check.  If that isn't possible, you can get dictionary.Keys, use LINQ's ToList() to convert this to an IList copy, and then iterate over that....


    Reed Copsey, Jr. - http://reedcopsey.com
    Wednesday, June 3, 2009 7:40 PM
    Moderator
  • thx guys but i've figured how to solve it using manual reset events

    This topic should help anyone with the same problem:

    http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/8b5e3f04-9be3-4285-a9d7-3c6e20f77c2d/

    Thursday, June 4, 2009 12:05 AM