0

I have issues with sorting an array of values. It does not matter what type of array mechanism I use, I always get errors like "Object reference not set to an instance of an object" while using Tuple, or "Value cannot be null" while sorting Dictionary.

The interesting thing is that the errors I mentioned above occur only while I use the parallel loop to process data faster. When I don't use a parallel loop, the data is sorted without any errors.

Here is a line that sorts Dictionary:

  DictionaryValues = DictionaryValues.OrderBy(Function(x) x.Value).ToDictionary(Function(x) x.Key, Function(x) x.Value)

I also tried Tuple for this, and here is a Tuple sorting line:

  lstToSort = lstToSort.OrderBy(Function(i) i.Item2).ToList

Finally, here is my "Parallel.For" block of code:

  Dim ProcessedCTDataValues As New Dictionary(Of String, Double)
  Dim t As Task = Task.Run(Sub()
                                     Parallel.For(0, dv.Count - 1,
                 Sub(iii As Integer)
                     Dim cmprName As String = dv(iii)(0).ToString & "; " & dv(iii)(1).ToString & "; " & dv(iii)(2).ToString & "; " & dv(iii)(3).ToString
                     Dim cmprAddress As String = dv(iii)(2).ToString

                     If Not ProcessedCTDataValues.ContainsKey(cmprName) Then
                         Dim similarityName As Double = 0
                         Dim similarityAddress As Double = 0

                         similarityName += GetSimilarity.Invoke(InputKeyword.ToLower, cmprName.ToLower)
                         similarityAddress += GetSimilarity.Invoke(StreetNumber.Invoke.ToLower, cmprAddress.ToLower)

                         If cmprName.ToLower.Contains(InputKeyword.ToLower) Then similarityName += 1
                         If InputKeyword.ToLower.Contains(cmprName.ToLower) Then similarityName += 1

                         For Each word As String In InputKeyword.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
                             If cmprName.ToLower.Contains(word.ToLower) Then similarityName += 0.3
                         Next

                         ProcessedCTDataValues.Add(cmprName, similarityName + similarityAddress)
                     End If
                 End Sub)
                                 End Sub)
        t.Wait()

Again, if I don't use a parallel loop, I can sort data without any issues. But if I use this loop, I always get an error. Any help would be greatly appreciated! Thanks!

1 Answer 1

1

To answer my own question - this is a very specific problem, which can be solved by using a Concurrent dictionary.

From Microsoft Docs: "Represents a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently."

Since I am using Parallel, this was a perfect solution to look forward to.

Instead of using Dictionary to add keys and values while using "Parallel.For", you can create a Concurrent dictionary, add items to it in the same way you would add to a regular dictionary, and use it in the same way as well.

To declare a Concurrent dictionary, you can use:

Dim concDict As ConcurrentDictionary(Of String, Double) = New ConcurrentDictionary(Of String, Double)()

To add items to it, use:

concDict.GetOrAdd(cmprName, similarityName + similarityAddress)

The concurrent dictionary is part of the System.Collections.Concurrent Namespace

Cheers!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.