You need to create an
Action(Of Integer)
delegate given the name of a method on the current class:
Public Sub ParallelForEach(ByVal startOfIteration As Integer, ByVal endOfIteration As Integer, ByVal subFunctionName As String)
Const flags As BindingFlags = BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Static Or BindingFlags.Instance
Dim method As MethodInfo = Me.GetType().GetMethod(subFunctionName, flags, Nothing, New Type() { GetType(Integer) }, Nothing)
If method Is Nothing Then
Throw New ArgumentException(String.Format("Method '{0}' was not found.", subFunctionName), "subFunctionName")
End If
Dim del As System.Delegate
If method.IsStatic Then
del = System.Delegate.CreateDelegate(GetType(Action(Of Integer)), method)
Else
del = System.Delegate.CreateDelegate(GetType(Action(Of Integer)), Me, method)
End If
Dim action As Action(Of Integer) = DirectCast(del, Action(Of Integer))
Dim numbers As IEnumerable(Of Integer) = Enumerable.Range(startOfIteration, 1 + endOfIteration - startOfIteration)
System.Threading.Tasks.Parallel.ForEach(numbers, processorCount, action)
End Sub
NB: The parameters to
Enumerable.Range
are
Integer
(32-bit), not
Long
(64-bit).
Also, the second parameter is the number of items to return,
not the last number to return. Your parameter names suggest you're passing in the last number to return, so you have to adjust for that.
Since you're iterating over a fixed set of numbers, it would probably be better to use
Parallel.For[
^] instead of
Parallel.ForEach
.