Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: chunkLength

StackOverflow https://stackoverflow.com/questions/23524524

Question

I am trying to use Parallel.ForEachLoop for a iterations of almost million records in My windows application. I am facing an error while fails on a convert to string on a string builder due to some threading problem although i use a object for lock. I Tried looking at shared resource for Parallel.ForEach could not find a proper answer.

dtProd has 900 000 records

Dim sbFile As New StringBuilder
messagesLock As Object = New Object()
Dim sbRecord As New StringBuilder
Dim dtDet As New Data.DataTable
Dim dtProd As New Data.DataTable
Public Sub CreateFeedFile()
    Try
    GetData()
    Dim temporaryEnumerable As IEnumerable(Of DataRow) = dtProd.Rows.Cast(Of DataRow)()
    sbRecord.AppendLine(dtFeed(0).Item("HeaderText"))

    Parallel.ForEach(temporaryEnumerable, Sub(dtDet)
                                                  RunLoop(DetCount)
                                              End Sub)

    sbRecord.AppendLine(dtFeed(0).Item("FooterText"))
    Catch ex As Exception
        Dim a = ex.Message.ToString()
    End Try
End Sub
 Private Sub RunLoop(ByRef DetCount As Integer)

 For Each drDet As DataRowView In dvDet 'loop detail records of field values 
 .. .. .. Append info to sbRecord
 Next
Try
        SyncLock Me.messagesLock
            sbFile.AppendLine(sbRecord.ToString())
            sbRecord.Clear()
        End SyncLock
    Catch ex As Exception
        Dim a = ex.Message.ToString() --Fails Here on the statement sbRecord.ToString()
    End Try
Était-ce utile?

La solution

The problem is that you have one StringBuilder (sbRecord) that is shared by the parallel threads. You need to move sbRecord to be a local variable inside RunLoop.

I think you also want the .AppendLine(...) calls in CreateFeedFile to be on sbFile rather than sbRecord.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top