Click here to Skip to main content
15,038,345 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to divide for loop between Tasks.

Sample code that should describe my idea:

for(int i = 0; i < array.Length; i++) 
   array[i].Function(); //void method

And I can't solve two issues:
1. How to divide it knowing that depending on chunk count, chunk size can be even or odd.
2. How to declare Tasks knowing the above.

What I have tried:

I tried a bunch of things but none of it actually worked as expected. Someone definitely knows how to handle it, and I would really appreciate the help. Already got banned on stackoverflow for asking this, but maybe here people are not afraid of answering questions.
Updated 16-Jan-21 4:17am
Richard MacCutchan 16-Jan-21 9:32am
What exactly are you trying to achieve?
Member 15047625 16-Jan-21 9:34am
That void method does some complicated things. For example if array.Length is 10, i can divide it to 2 Tasks. First task will handle i 0 - 4, second 5 - 9. And it will be faster than sequence.
I have a feeling it should be easy, but for some reason i can't even divide it properly. I want to create tasks dynamically depending on size, and not declare X manually because it's terrible solution.
Richard MacCutchan 16-Jan-21 9:42am
It sounds like you need to calculate the size of each sub-array. So for array of size 10, you just get two 5 element chunks. You then create a task for each chunk and pass it the aray, the start offset of the chunk and its length. Something like:
int chunkSize = array.Length / 2;
Task first = new Task();
first.start(array, 0, chunkSize);
Task second = new Task();
second.start(array, chunkSize, chunkSize);

That is an over simplification but should give you the idea.
Member 15047625 16-Jan-21 9:46am
I know, i have manually made two Tasks already, but what if array.Length is 100 000, or 100 000 000. It begs to use more Tasks, but I'm not sure how to make it dynamically.
Richard MacCutchan 16-Jan-21 9:59am
You need to decide some maximum number of tasks and adjust the count up or down between 1 and the maximum based on the number of elements to process.

Member 15047625 16-Jan-21 9:53am
Parallel.For is somewhat equal to 1 Task per one iteration, and I had an idea to divide it to chunks.
RickZeeland 16-Jan-21 9:57am
Updated the solution with a "chunky" alternative :)
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));

public void DoChunk(int[] arr, int i, int 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!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900