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!