-2

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
}

For example if array count is 20, and I have set chunk count to 4, it will give us chunk size 5. First task will handle i from 0 to 4, second 5 to 9, 10 to 14, 15 to 19. And it's optimistic case, if number is odd, you need additional task to handle a few elements.

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.

Before someone says that I should try myself first: if I would know what to do, I would try. I'm asking because I don't know. This is the purpose of a forum. Wild idea, I know.

Perro12
  • 21
  • 5
  • Thank you for taking the time to share your question. What you asking for is unclear. What is your goal & difficulty? What have you done so far? Please try to better explain your issue, dev env, data types & expected result, as well as to share more or less code (no screenshot), images or sketches of screens, user stories or scenario diagrams. To help you improve your requests please read [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) & [Writing the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question). –  Jan 16 '21 at 14:12
  • I don't understand what you are talking about at all. Can you clarify the question and add more detail, please? What are the mentionned *tasks*? What do you mean by *divide*? What is *chunk count*? Also Stack Overflow is not a *forum*: it is a database where someone post questions about a problem and others try to answer and solve the problems to help coders and improve the world job on computing. Thus that need a minimal concrete DATA INPUT to do a PROCESS to get a RESULT. Without complete and accurate data to some extent, no process is possible, so no output... –  Jan 16 '21 at 14:18
  • Something like this? https://stackoverflow.com/questions/13709626/split-an-ienumerablet-into-fixed-sized-chunks-return-an-ienumerableienumerab – Mike Christiansen Jan 16 '21 at 14:28
  • @MikeChristiansen I also had similar idea but there is no reason to split array itself, since it's void function. – Perro12 Jan 16 '21 at 14:47
  • @Perro12 - [I made ananswer](https://stackoverflow.com/a/65761473/29249) that may clarify what I'm talking about. – Mike Christiansen Jan 17 '21 at 13:59

1 Answers1

0

OP's text is kinda confusing / vague. So, I'm going to take this section of the OP, and attempt to answer it in a couple different ways.

For example if array count is 20, and I have set chunk count to 4, it will give us chunk size 5. First task will handle i from 0 to 4, second 5 to 9, 10 to 14, 15 to 19.

Note, that I will be using the extension method Batch as defined on this other post


If we want to call a single method for each 'chunk'

void PerformActionsOnAllItems(IEnumerable<string> items)
{
    int chunkSize = 5;
    foreach(IEnumerable<string> chunk in items.Batch(chunkSize))
    {
        PerformAction(chunk);
    }
}

void PerformAction(IEnumerable<string> chunkItems)
{
    Console.WriteLine("---- CHUNK -----");
    foreach(string item in chunkItems)
    {
        Console.WriteLine("Item: " + item);
    }
}

If we want to alternate between which method is used for each 'chunk'

void PerformActionsOnAllItems(IEnumerable<string> items)
{
    int chunkSize = 5;
    int chunkNumber = 0;
    foreach(IEnumerable<string> chunk in items.Batch(chunkSize))
    {
        if((chunkNumber % 2) == 0)
        {
            PerformEvenAction(chunk);
        }
        else
        {
            PerformOddAction(chunk);
        }
        ++chunkNumber;
    }
}

void PerformEvenAction(IEnumerable<string> chunkItems)
{
    Console.WriteLine("---- EVEN CHUNK -----");
    foreach(string item in chunkItems)
    {
        Console.WriteLine("Even Item: " + item);
    }
}

void PerformOddAction(IEnumerable<string> chunkItems)
{
    Console.WriteLine("---- ODD CHUNK -----");
    foreach(string item in chunkItems)
    {
        Console.WriteLine("Odd Item: " + item);
    }
}
Mike Christiansen
  • 1,104
  • 2
  • 13
  • 30