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.GetStrea m();
nSize = NetStream.Read( Packet, 0, PACKET_SIZE);
sValue = Encoding.ASCII. GetString(Packe t, 0, nSize);
System.Windows. Forms.MessageBo x.Show(sValue);
}
}
}
}
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.GetStrea m();
nSize = NetStream.Read( Packet, 0, PACKET_SIZE);
sValue = Encoding.ASCII. GetString(Packe t, 0, nSize);
System.Windows. Forms.MessageBo x.Show(sValue);
}
}
}
}
Comment