Forum C# Collection was modified; enumeration operation may not execu

Collection was modified; enumeration operation may not execu

Postby wbsaputra » March 14th, 2011, 9:55 pm

need help with this kind of error, i keep getting this error while looping code "foreach player in players", this happen everytime when reseting game state. is because any player is leaving room while this code execute? how to handle this? :cry:
thanks for help
wbsaputra
Paid Member
 
Posts: 150
Joined: June 29th, 2010, 4:38 pm

Re: Collection was modified; enumeration operation may not execu

Postby phil.peron » March 16th, 2011, 7:21 pm

It usually means exactly what it says. Without seeing your code, I'd guess you're modifying the collection during the iteration. That would throw the error.
phil.peron
 
Posts: 35
Joined: September 24th, 2010, 7:50 pm

Re: Collection was modified; enumeration operation may not execu

Postby Oliver » March 18th, 2011, 10:52 am

You shouldn't get that exception from simply looping over the Players collection, since the Players collection is built to be enumerated while users enter and leave.

Are you looping over anything else, that you might be changing concurrently yourself?

- Oliver
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am

Re: Collection was modified; enumeration operation may not execu

Postby wbsaputra » March 25th, 2011, 4:53 pm

how can i built such enumerate type like players object so i can freely looping that object and freely remove that object item while looping executed? :)
wbsaputra
Paid Member
 
Posts: 150
Joined: June 29th, 2010, 4:38 pm

Re: Collection was modified; enumeration operation may not execu

Postby Oliver » March 25th, 2011, 4:57 pm

You can freely loop over the Player collection and call .Disconnect as you want.

For other collections you have to build a list of stuff to remove and remove them after the foreach OR use a for(int i=0;i!=....) loop
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am

Re: Collection was modified; enumeration operation may not e

Postby tripknotix » October 26th, 2015, 12:56 pm

I actually still get this get exact error, it seems to happen only about once per day.

Do i need to copy over the player list before hand?
tripknotix
Paid Member
 
Posts: 28
Joined: March 12th, 2010, 11:00 am


Return to C#



cron