0

In my C# project I have to open a bunch of images.

Let's say we need to open 50. My plan is to create 10 Tasks, do some stuff, and then wait for each to complete before the next 10 Tasks are created.

var fd = new OpenFileDialog
{
    Multiselect = true,
    Title = "Open Image",
    Filter = "Image|*.jpg"
};

using (fd)
{
    if (fd.ShowDialog() == DialogResult.OK)
    {
       int i = 1;                
       foreach (String file in fd.FileNames)
       { 
           if (i <= 10) {
               i++;
               Console.WriteLine(i + ";" + file);
               Task task = new Task(() =>
               {
                   // do some stuff
               });
               task.Start();
           } 
           else
           {
               Task.WaitAll();
               i = 1;
           } 
       }  
    }
}
Console.WriteLine("Wait for Tasks");
Task.WaitAll();
Console.WriteLine("Waited);

The Code is not waiting when i=10 and at the end it is also not waiting. Does anyone have an idea how to fix it?

5

1 Answer 1

4

Task.WaitAll expects a Task array to wait, you never pass anything in. The following change will wait all the tasks you start.

List<Task> tasksToWait = new List<Task>();
foreach (String file in fd.FileNames)
{ 
   if (i <= 10) {
       i++;
       Console.WriteLine(i + ";" + file);
       Task task = new Task(() =>
       {
           // do some stuff
       });
       task.Start();
       tasksToWait.Add(task);
   } 
   else
   {
       Task.WaitAll(tasksToWait.ToArray());
       tasksToWait.Clear();
       i = 1;
   } 
} 

This is a code fragment from your code above that has changes


Note This answer does not contain a critique on your choice of design and the possible pitfalls thereof.

2
  • Thanks Igor! I know my code could be crap, but it is okay for me. Your code helped me a lot. Thanks!
    – Schlodi
    Mar 13, 2018 at 18:42
  • Task.WaitAll just excepts Array List. Thus, you should call like Task.WaitAll(tasksToWait.ToArray())
    – Engineert
    Sep 11, 2019 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.