【问题标题】:What is the correct syntax using VB.Net Parallel.ForEach with ConcurrentDictionary?使用带有 ConcurrentDictionary 的 VB.Net Parallel.ForEach 的正确语法是什么?
【发布时间】:2015-01-29 23:26:10
【问题描述】:

我很难使用 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 行。为了清楚起见,我已经包括了其他定义,但也许它只会引起更多的混乱。大声笑

标签: vb.net parallel-processing parallel.foreach


【解决方案1】:

看起来您正在尝试调用 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泛型参数是从参数中推断出来的,所以不需要在方法调用时指定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-22
    • 2013-11-21
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 2018-11-14
    相关资源
    最近更新 更多