在 ConcurrentDictionary 中使用 VB.Net Parallel.ForEach 的正确语法是什么?
What is the correct syntax using VB.Net Parallel.ForEach with ConcurrentDictionary?
我很难使用 Parallel.ForEach 和 ConcurrentDictionary 获得正确的语法。以下 Parallel.ForEach 的正确语法是什么?
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)
看起来您正在尝试调用 Parallel.ForEach(OF TSource)(IEnumerable(Of TSource), Action(Of TSource))
重载,在这种情况下,我相信您想要这样的东西:
'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
)
您需要迭代 ServerList
字典的 Values
,其类型为 Server
。 TSource
泛型参数是从参数中推断出来的,所以你不需要在方法调用时指定它。
我很难使用 Parallel.ForEach 和 ConcurrentDictionary 获得正确的语法。以下 Parallel.ForEach 的正确语法是什么?
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)
看起来您正在尝试调用 Parallel.ForEach(OF TSource)(IEnumerable(Of TSource), Action(Of TSource))
重载,在这种情况下,我相信您想要这样的东西:
'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
)
您需要迭代 ServerList
字典的 Values
,其类型为 Server
。 TSource
泛型参数是从参数中推断出来的,所以你不需要在方法调用时指定它。