0

このコードを に変換するにはどうすればよいParallel.ForEach (WorkingDict is a HybridDictionary)ですか?

以下に私のコードを含めました (私の方法を判断しないでください。このツールはクラスでの私の最初の試みであり、かなり複雑で作業が面倒だと思い始めています)。

Dim Cntr As Integer = 0, Bool1 As Boolean = False
For Each Obj As Object In BusLoadDict.Keys
    Cntr = 0
    Bool = False
    bool1 = False
    For Each Obj2 As Object In PNodesDict.Keys
        For Each Obj1 As Object In DirectCast(PNodesDict.Item(Obj2.ToString), PNodeClass) _
            .PNodeDescriptionDictOfDicts.Keys
            If Obj1.ToString.Contains("forecast zone") Then
                If DirectCast(DirectCast(DirectCast(PNodesDict.Item(Obj2.ToString),  _
                            PNodeClass).PNodeDescriptionDictOfDicts, HybridDictionary).Item("forecast zone"),  _
                    HybridDictionary).Contains(BusLoadDict.Item(Obj.ToString)) Then
                    Bool = True
                    Cntr += 1
                    If Cntr = 2 Then GoTo 1
                End If
            End If
            If Obj1.ToString.Contains("aggregate") Then
                Bool1 = True
                If DirectCast(DirectCast(DirectCast(PNodesDict.Item(Obj2.ToString),  _
                            PNodeClass).PNodeDescriptionDictOfDicts, HybridDictionary).Item("aggregate"),  _
                    HybridDictionary).Contains(BusLoadDict.Item(Obj.ToString)) Then
                    Bool = True
                    Cntr += 1
                    If Cntr = 2 Then GoTo 1
                End If
            End If
        Next
    Next
1:          If Bool = False Then
    SBuilder.AppendLine("Bus PNode " & Obj.ToString & " was not mapped to the forecast zone pnodes.")
    End If
    If bool1 = False Then
        SBuilder.AppendLine("Bus PNode " & Obj.ToString & " was not mapped to the aggregate pnodes.")
    End If
Next

これがエラーコードです。

Error   1   Overload resolution failed because no accessible 'ForEach' can be called with these arguments:
    'Public Shared Function ForEach(Of TSource)(source As System.Collections.Concurrent.OrderablePartitioner(Of TSource), body As System.Action(Of TSource, System.Threading.Tasks.ParallelLoopState, Long)) As System.Threading.Tasks.ParallelLoopResult': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
    'Public Shared Function ForEach(Of TSource)(source As System.Collections.Concurrent.Partitioner(Of TSource), body As System.Action(Of TSource, System.Threading.Tasks.ParallelLoopState)) As System.Threading.Tasks.ParallelLoopResult': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
    'Public Shared Function ForEach(Of TSource)(source As System.Collections.Concurrent.Partitioner(Of TSource), body As System.Action(Of TSource)) As System.Threading.Tasks.ParallelLoopResult': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
    'Public Shared Function ForEach(Of TSource)(source As System.Collections.Generic.IEnumerable(Of TSource), body As System.Action(Of TSource, System.Threading.Tasks.ParallelLoopState, Long)) As System.Threading.Tasks.ParallelLoopResult': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
    'Public Shared Function ForEach(Of TSource)(source As System.Collections.Generic.IEnumerable(Of TSource), body As System.Action(Of TSource, System.Threading.Tasks.ParallelLoopState)) As System.Threading.Tasks.ParallelLoopResult': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
    'Public Shared Function ForEach(Of TSource)(source As System.Collections.Generic.IEnumerable(Of TSource), body As System.Action(Of TSource)) As System.Threading.Tasks.ParallelLoopResult': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error. C:\My Folder\Market Operations\VB.net\Market Validations Tool\Market Validations Tool\Market Operations Tool.vb 6361    9   Market Validations Tool
4

1 に答える 1

2

はい、このようにできます。

Dim WorkingDict As New HybridDictionary
WorkingDict.Add("k1", "v1")
WorkingDict.Add("k2", "v2")
Parallel.ForEach(WorkingDict.Cast(Of Object),
                 Sub(DictObject As KeyValuePair(Of String, String))
                     ' do something
                 End Sub)

ジェネリック コレクションでのみ機能するIEnumerable.Castため、演算子を使用する必要があります。非ジェネリック コレクションです。Parallel.Foreach HybridDictionary

ただし、これは非常に単純な [空の] 例であり、より複雑な並列操作では、共有リソースのロックなど、多くの考慮事項が必要になることに注意してくださいSystem.Collections.ConcurrentDictionary。必要に応じて、共有リソースのロックを処理する を常に使用することを検討できます。これに関するSOの投稿。例:

Dim WorkingDict As New ConcurrentDictionary(Of String, String)
' ConcurrentDictionary has no Add operator, so to replicate it, use AddOrUpdate
' and just pass the same value whether it is being added or updating
WorkingDict.AddOrUpdate("key1", "value1", Function(key, oldvalue) "value1")
WorkingDict.AddOrUpdate("key2", "value2", Function(key, oldvalue) "value2")
Parallel.ForEach(WorkingDict,
                 Sub(DictObject as DictionaryEntry)
                     ' do something
                 End Sub)
于 2013-03-06T02:53:16.247 に答える