In your example code, you need to initialize the array to the size of pRowDatas
:
Dim TempR(0 To pRowDatas.Count-1) As KeyValuePair(Of Integer, RowData)
pRowDatas.CopyTo(TempR, 0)
There's also a ToArray
extension method that you could use:
Dim TempR As KeyValuePair(Of Integer, RowData) = pRowDatas.ToArray()
Neither of these are inherently thread-safe. The proper way to do this would be to implement a lock both here and in the utility code. Since you have no access to the code, it seems your options are limited here, but someone else may have some idea.