Set up a function which accepts a start index and a items count, and use it to run a
for
loop.
Then call that function inside a thread, and start several threads to complete the task.
Something like this:
int[] myArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int chunkSize = 5;
for (int startIndex = 0; startIndex < myArray.Length; startIndex += chunkSize)
{
int sIndex = startIndex;
System.Threading.Thread t = new System.Threading.Thread(() => DoChunk(myArray, sIndex, chunkSize));
t.Start();
}
public void DoChunk(int[] arr, int i, int s)
{
Console.WriteLine($"{i}:{s}");
while (i < arr.Length && s-- > 0)
{
arr[i++] += 100;
}
}
You do need the temporary variable inside the thread creating loop, or you won't get the results you expect!