Michael Webb
Michael Webb

Reputation: 31

system.invalidoperationexception: collection was modified; enumeration operation may not execute. Need ideas?

I'm getting this error from my C# Emulator,

Error en thread Room cycle task for room 1: 
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.HashSet`1.Enumerator.MoveNext()
at Mercury.HabboHotel.Rooms.RoomManager.UnloadRoom(Room Room) in                        xampp\Emulator\HabboHotel\Rooms\RoomManager.cs:line 629
at Mercury.HabboHotel.Rooms.Room.ProcessRoom() in c:\xampp\Emulator\HabboHotel\Rooms\Room.cs:line 807

Quick explanation, this emulator is for a flash game. The code it's referring too is such. RoomManager.cs line 629;

lock (Room.RoomChat)
            {
                foreach (Chatlog current2 in Room.RoomChat)
                {
                    current2.Save(Room.RoomId);
                }
            }
            Room.Destroy();

It's the foreach line. Room.cs line 807,

if (this.IdleTime >= 60/* && usersQueueToEnter.Count == 0*/)
                    {
                        MercuryEnvironment.GetGame().GetRoomManager().UnloadRoom(this);
                        mIsIdle = false;
                        return;
                    }

It's the MercuryEnvironment.GetGame() Line to be exact. Basically, the first void I showed is meant to be saving a room chatlog and then 'destroying' the room which is just meaning no one is in it. The second part refers to an idle timer. If your character goes idle for too long, they update with a little 'Zzz' next to them, after a while they get kicked from the room. So, I'm not sure what is going on with this problem. Anyone have any ideas?

Upvotes: 2

Views: 2942

Answers (1)

Jauch
Jauch

Reputation: 1518

You can't remove an item from a collection during a foreach.

Edit

The exception is been throwing when you remove the 'room' from the collection you are iterating in the foreach. There would be a problem with the internal counter.

As pointed, using Reverse in the foreach might work.

Upvotes: 2

Related Questions