koryakinp
koryakinp

Reputation: 4125

Run tasks in a batch

I am trying to understand System.Task, but I am not sure if what I am doing is correct. My goal is to write a method to process images parallel in a batch.

Is my understanding correct, that in case of batchSize = 3 the method will queue 3 tasks and run all 3 tasks in parallel as soon as Task.WaitAll() being called ?

Is there are more elegant way if doing it ?

private static void ProcessImages(int batchSize)
{
      List<Task> tasks = new List<Task>();
      foreach (var image in ImageSource.ReadImages())
      {
            if(tasks.Count < batchSize)
            { 
                 tasks.Add(Task.Run(() => ImageProcessor.ProcessImage(image)));
            }
            else
            {
                 Task.WaitAll(tasks.ToArray());
                 tasks.Clear();
            }
      }
}

Upvotes: 1

Views: 1741

Answers (2)

Enigmativity
Enigmativity

Reputation: 117009

Another option is to use Microsoft's Reactive Framework (NuGet "System.Reactive").

Then this works:

ImageSource
    .ReadImages()
    .ToObservable()
    .Select(image => Observable.Start(() => ImageProcessor.ProcessImage(image)))
    .Merge(maxConcurrent: 3)
    .Wait();

IMHO the Reactive Framework is far more powerful than tasks.

Upvotes: 2

Ňuf
Ňuf

Reputation: 6207

Is my understanding correct, that in case of batchSize = 3 the method will queue 3 tasks and run all 3 tasks in parallel as soon as Task.WaitAll() being called ?

While the idea to add tasks to the list and then Task.WaitAll() these tasks is correct, your code is unfortunatelly buggy and will not work as expected. Specifically, it will NOT execute tasks for images that follows after each batch and also it will NOT execute tasks for images in the last batch, if number of images is not divisible by batchSize+1.

Is there are more elegant way if doing it ?

Fortunatelly, executing tasks in batches is very common requirement, so .NET already contains methods that makes it much easier. With PLINQ it is as simple as this:

ImageSource.ReadImages()
    .AsParallel()
    .WithDegreeOfParallelism(batchSize)
    .ForAll(image => ImageProcessor.ProcessImage(image));

Upvotes: 6

Related Questions