Try using a
Tuple
for the local state:
Parallel.ForEach(files, Function() (Length := 0L, Count := 0L),
Function(f As FileInfo, loopState As ParallelLoopState, index As Long, accumulator As (Length As Long, Count As Long))
If (f.Attributes And FileAttributes.Directory) = 0 Then
accumulator = (accumulator.Length + f.Length, accumulator.Count + 1)
End If
Return accumulator
End Function,
Sub(total As (Length As Long, Count As Long))
Interlocked.Add(total_bytes, total.Length)
Interlocked.Add(total_files, total.Count)
BeginInvoke(CType(Sub()
pbVal = progBar.Value + increm
If pbVal > 100 Then pbVal = 100
progBar.Value = pbVal
End Sub, Action))
End Sub)
Tuples - Visual Basic | Microsoft Docs[
^]