473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Collection was modified?!

When executing the following function, I get this error message:

Collection was modified; enumeration operation may not execute.

The only thing that can modify lstClients is when I add one in it and I'm
sure that my listener isn't accepting another connection. As you can see,
I even locked it!!! What is wrong?

private void ReceiveMessage()
{
byte[] Packet = new byte[PACKET_SIZE];
string sValue;
int nSize;

while (Started)
{
lock (lstClients)
{
foreach (TcpClient Client in lstClients)
{
NetworkStream NetStream = Client.GetStream();

nSize = NetStream.Read(Packet, 0, PACKET_SIZE);
sValue = Encoding.ASCII.GetString(Packet, 0, nSize);
System.Windows.Forms.MessageBox.Show(sValue);
}
}
}
}

Aug 22 '08 #1
1 3164
On Fri, 22 Aug 2008 14:22:50 -0700, Michel Racicot <mr******@hotmail.com>
wrote:
When executing the following function, I get this error message:

Collection was modified; enumeration operation may not execute.

The only thing that can modify lstClients is when I add one in it and I'm
sure that my listener isn't accepting another connection. As you can
see,
I even locked it!!! What is wrong?
Locking is a cooperative affair. That is, all that using the "lock()"
statement does is ensure that while the code in the locked block is
executing, no other code in a block that locks on the same object can
execute. There's nothing to stop code from just not using the "lock)"
statement and modifying the collection in an unsynchronized way.

So, most likely that's just what you've got somewhere. Code that modifies
the collection without use of the "lock()" statement. Find the code, add
the "lock()" statement, and it should work.

Pete
Aug 22 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
5
by: David C | last post by:
This is very strange. Say I have code like this. I am simply looping through a collection object in a foreach loop. Course course = new Course(); foreach(Student s in course.Students) {...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
3
by: Jeff L. | last post by:
I have an interesting problem and I'm not coming up with any answers in my searches, so hopefully someone can give me a hand with this. I have a feeling it's easy, but I usually get my nose stuck...
3
by: Franky | last post by:
IN vb.Net Class clsCommande Inherits CollectionBase and Class clsProduct clsCommand contain a IList of clsProduct
3
by: Steve Barnett | last post by:
I have a user control that includes a StringCollection. I provide my users with a property that retrieves a reference to the string collection so they can add and remove items from it, just as you...
0
by: Geoffrey | last post by:
Hello, I work with .net remoting,to simplify, when the client connected, he send an handle to an object, the object is used to exchange message and events. no problem. In my server, I keep a...
5
by: sstory | last post by:
I have a class in vb.net that Inherits Generic.List(Of myclass) I want to have totals in this class that are static...i.e. not recalculated every time. They should be calculated once and never...
3
by: usenet.tolomea | last post by:
I'm working on a remote object system, something kinda like Pyro. For the purposes of caching I need to be able to tell if a given dict / list / set has been modified. Ideally what I'd like is for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.