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?