I'm having difficulty getting the correct syntax using the Parallel.ForEach and a ConcurrentDictionary. What is the correct syntax for the Parallel.ForEach below?
Dim ServerList as New ConcurrentDictionary(Of Integer, Server)
Dim NetworkStatusList as New ConcurrentDictionary(Of Integer, NetworkStatus)
... (Fill the ServerList with several Server class objects)
'Determine if each server is online or offline. Each call takes a while...
Parallel.ForEach(Of Server, ServerList, Sub(myServer)
Dim myNetworkStatus as NetworkStatus = GetNetworkStatus(myServer)
NetworkStatusList.TryAdd(myServer.ID, myNetworkStatus)
End Sub
... (Output the list of server status to the console or whatever)
It looks like you are trying to call the Parallel.ForEach(OF TSource)(IEnumerable(Of TSource), Action(Of TSource))
overload, in which case I believe you want something like this:
'Determine if each server is online or offline. Each call takes a while...
Parallel.ForEach(
ServerList.Values,
Sub(myServer)
Dim myNetworkStatus as NetworkStatus = GetNetworkStatus(myServer)
NetworkStatusList.TryAdd(myServer.ID, myNetworkStatus)
End Sub
)
You need to iterate over the Values
of your ServerList
dictionary, which are of type Server
. The TSource
generic parameter is inferred from the parameters, so you don't need to specify it on the method call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With