G
gregory_may
I keep getting an error:
Collection was modified; enumeration operation may not execute.
Error from the following code. I cant seem to get it to properly syncronize
the Clients object (Its a sorted list). Any suggestions:
Private Sub SendToClients(ByVal strMessage As String, ByVal sender As
UserConnection)
Dim client As UserConnection
Dim entry As DictionaryEntry
Dim SyncClients As SortedList
Try
'Need to lock the collection. Threads can be modifying the clients
collection
'While we are broadcasting. Very bad things can happen.
'Synclock .SyncRoot locks the collection for us.
SyncLock clients.SyncRoot
SyncClients = CType(clients.SyncRoot, SortedList)
For Each entry In SyncClients
'Make sure we have someone to send the message too.
If Not IsNothing(entry.Value) Then
client = CType(entry.Value, UserConnection)
' Exclude the sender.
If client.NetworkName <> sender.NetworkName Then
GracefulSendMessage(strMessage, client)
End If
End If
Next
End SyncLock
Catch ex As Exception
UpdateStatus("-------------------------")
UpdateStatus("ServerComm - SendToClients - Error: " & ex.Message)
UpdateStatus("Message: " & strMessage)
UpdateStatus("To: " & sender.NetworkName)
UpdateStatus("-------------------------")
End Try
End Sub
Collection was modified; enumeration operation may not execute.
Error from the following code. I cant seem to get it to properly syncronize
the Clients object (Its a sorted list). Any suggestions:
Private Sub SendToClients(ByVal strMessage As String, ByVal sender As
UserConnection)
Dim client As UserConnection
Dim entry As DictionaryEntry
Dim SyncClients As SortedList
Try
'Need to lock the collection. Threads can be modifying the clients
collection
'While we are broadcasting. Very bad things can happen.
'Synclock .SyncRoot locks the collection for us.
SyncLock clients.SyncRoot
SyncClients = CType(clients.SyncRoot, SortedList)
For Each entry In SyncClients
'Make sure we have someone to send the message too.
If Not IsNothing(entry.Value) Then
client = CType(entry.Value, UserConnection)
' Exclude the sender.
If client.NetworkName <> sender.NetworkName Then
GracefulSendMessage(strMessage, client)
End If
End If
Next
End SyncLock
Catch ex As Exception
UpdateStatus("-------------------------")
UpdateStatus("ServerComm - SendToClients - Error: " & ex.Message)
UpdateStatus("Message: " & strMessage)
UpdateStatus("To: " & sender.NetworkName)
UpdateStatus("-------------------------")
End Try
End Sub