gpt4 book ai didi

vb.net - 使用 VB.Net Parallel.ForEach 和 ConcurrentDictionary 的正确语法是什么?

转载 作者:行者123 更新时间:2023-12-04 02:03:45 24 4
gpt4 key购买 nike

我很难使用 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 泛型参数是从参数中推断出来的,因此您不需要在方法调用时指定它。

关于vb.net - 使用 VB.Net Parallel.ForEach 和 ConcurrentDictionary 的正确语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28226621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com