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
You should use one of the ConcurrentCollections provided by .NET, eg ConcurrentBag or ConcurrentQueue.
See here: https://msdn.microsoft.com/en-us/library/dd997305%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.