Hey guys!

So I've been working on a socket server for a game for some time now. It has your typical instant messaging and chatting as well as racing. It's multithreaded as each socket connection is a different thread as well as a maintenance thread that works in the background to cleanup objects that are sitting around (namely stuff like race requests that were never accepted or declined).


The problem I'm running into is that I'm doing a lot of enumerating. Reason being is I have generic lists and dictionary objects of races, clients, chatrooms, etc. And they're constantly changing. So, just recently because of testing we had more users that normal on doing lots of operations and I received the following error here and there:

Collection was modified; enumeration operation may not execute.

Of course this was a pain to track because it happens when something is trying to add/remove something from a collection whilst the collection is being enumerated. After doing some reading, it looked like the best option was to use SyncLock, however, that would be a performance hog. I do recall reading somewhere just to make a copy of the reference object and enumerate through that. Would this be an ideal situation? Which is less of a performance hog? Is there a better option? I'm pretty far along in this application, and I know I'm going to have to make some changes wherever there is a For Each that has to do with objects that are being touched by other users because of this.

Please advise!

Thanks all,
-Steve