Reputation: 915
I'm storing TcpClient objects in a list, once client connects. When broadcasting to all connected clients, loop through the list of those and send data. The problem sometimes occurs while in a loop, throwing exception "Enumeration modified:" when one of those clients disconnects and its object is being disposed.
What would be a solution to that? Or simply store NetworkStream instead?
Thanks
Upvotes: 1
Views: 169
Reputation: 9499
You should use one of the ConcurrentCollections provided by .NET, e.g. ConcurrentBag or ConcurrentQueue.
See here: https://msdn.microsoft.com/en-us/library/dd997305%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Upvotes: 1