使用 VB.Net Parallel.ForEach 和 ConcurrentDictionary 的正确语法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28226621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the correct syntax using VB.Net Parallel.ForEach with ConcurrentDictionary?
提问by user1532208
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?
我很难使用 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)
回答by Mark
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:
看起来您正在尝试调用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
)
You need to iterate over the Valuesof your ServerListdictionary, which are of type Server. The TSourcegeneric parameter is inferred from the parameters, so you don't need to specify it on the method call.
您需要遍历Values您的ServerList字典的 ,它们的类型是Server。该TSource泛型参数从参数推断,所以你并不需要指定它的方法调用。

