Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 648
user1532208 Avatar asked Oct 16 '25 18:10

user1532208


1 Answers

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.

like image 172
Mark Avatar answered Oct 18 '25 21:10

Mark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!