What is the correct syntax using VB.Net Parallel.ForEach with ConcurrentDictionary?

10,761

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.

Share:
10,761
user1532208
Author by

user1532208

Updated on June 04, 2022

Comments

  • user1532208
    user1532208 almost 2 years

    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)