|
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 - Edited byHungerDeveloper Tuesday, June 02, 2009 8:38 PM
- Edited byHungerDeveloper Wednesday, June 03, 2009 11:57 PM
-
| | 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. - Marked As Answer byZhi-Xin YeMSFT, ModeratorTuesday, June 09, 2009 11:57 AM
- Proposed As Answer bybsoulier Wednesday, June 03, 2009 9:54 AM
-
| | 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. - Marked As Answer byZhi-Xin YeMSFT, ModeratorTuesday, June 09, 2009 11:57 AM
- Proposed As Answer bybsoulier Wednesday, June 03, 2009 9:54 AM
-
| | 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 |
|