.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Collection Modified Exception During Multi-Thread Session
 

Collection Modified Exception During Multi-Thread Session

Ive got a collection modified Exceptionin a foreach loopwhich 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
HungerDeveloper

Hi again,

The only importzant thing is to parse your Dictionary without the foreach loop, this way the foreach sequence will not be broken if you insert a vlue in your Dictionary on another thread.


Try this as an example:

            int numerOfInsertsInForLoop = 2;
            Dictionary<string, object> myDictionary = new Dictionary<string, object>();
            myDictionary.Add("one", 1);
            myDictionary.Add("two", 2);
            myDictionary.Add("three", 3);
            myDictionary.Add("four", 4);
            myDictionary.Add("five", 5);
            for (int i = 0; i < myDictionary.Keys.Count; i++)
            {
                if (numerOfInsertsInForLoop != 0)
                {
                    myDictionary.Add("new" + numerOfInsertsInForLoop.ToString(), "new");
                    numerOfInsertsInForLoop--;
                }
                Debug.WriteLine(myDictionary[myDictionary.ElementAt(i).Key]);
            }

Here I'm just adding values inside my dictionary, and because I don't use foreach, I can go on and loop into my collection without a problem.

Hope this helps!

Ben.

bsoulier
maybe its a matter of placing "lock"s in the right places... i just dont know whatthe real problem is, someone help me plz!

HungerDeveloper
If I am not wrong, the problem here is that you are modifying list of items inside your Dictionary.
Any operation modifying the enumeration during a foreach is making a GetEnumerator() on collection to crash.
I would suggest to parse items on your Dictionary with a standart For loop, and this will do the job.

Hope this helps!
  • Proposed As Answer bybsoulier Tuesday, June 02, 2009 8:37 PM
  •  
bsoulier

how can i do that? i'm kinda begginer... for loops work for arrays sohow can i turn the dictionary into an array?

HungerDeveloper
You don't need to do that, just use your Dictionary like an array to parse it.
Here's an example:

for (int i=myDico.Keys.Count; i>0; i--)
{
myDico.RemoveAt(i);
}

Sorry for the code, but I just wrote it without Visual Studio, but this is the idea :)
  • Proposed As Answer bybsoulier Tuesday, June 02, 2009 8:56 PM
  •  
bsoulier

hey man
i dont know if im doing it wrong but this isnt working

after Remove, it asks for a string, not an int...

HungerDeveloper

Hi again,

The only importzant thing is to parse your Dictionary without the foreach loop, this way the foreach sequence will not be broken if you insert a vlue in your Dictionary on another thread.


Try this as an example:

            int numerOfInsertsInForLoop = 2;
            Dictionary<string, object> myDictionary = new Dictionary<string, object>();
            myDictionary.Add("one", 1);
            myDictionary.Add("two", 2);
            myDictionary.Add("three", 3);
            myDictionary.Add("four", 4);
            myDictionary.Add("five", 5);
            for (int i = 0; i < myDictionary.Keys.Count; i++)
            {
                if (numerOfInsertsInForLoop != 0)
                {
                    myDictionary.Add("new" + numerOfInsertsInForLoop.ToString(), "new");
                    numerOfInsertsInForLoop--;
                }
                Debug.WriteLine(myDictionary[myDictionary.ElementAt(i).Key]);
            }

Here I'm just adding values inside my dictionary, and because I don't use foreach, I can go on and loop into my collection without a problem.

Hope this helps!

Ben.

bsoulier
hey dude thx! but i think i forgot to mention im using C#! this "Debug.WriteLine(myDictionary[myDictionary.ElementAt(i).Key]);" command, specially elementat(i) doesn't exist in the collections library!


HungerDeveloper
This was just an example to display row values, you can remove it in your case:)
bsoulier

You can use google to search for other answers

Custom Search

More Threads

• Error Handling in ASP
• Standard DateTime Format with milliseconds
• Q: DIA SDK not finding SymTagUDT in .NET 2.0 programs
• Change in type of datagridview cell from textbox to CheckBox
• SendKeys and DirectX
• Way to not create cmd prompt and its output when the console exe get called within another console app?
• How to add an additional field in the existing database table and adding an additional table in the existing database
• Edit Entire Listview
• Unexplained CryptographicException : Padding is invalid...
• serializing just the base class