WaitAll not working. I get Done before tasks are completed.
I have some groups. each group contains items i want each item to be processed in a separate task.
static void Main(string[] args) { List<Task> TaskList = new List<Task>(); foreach (string group in groups) { Task task = new Task(() => { scan_group(group, timeout); }); task.Start(); TaskList.Add(task); } Task.WaitAll(TaskList.ToArray()); Console.WriteLine("- Done !" ); } public static void scan_group (){ Task.Factory.StartNew(() => Parallel.ForEach<string>(items, x => { scan(x, y); })); }
Parallel.ForEachdoesn't serve any useful purpose. There's no reason to create cold tasks, especially when you call Start in the very next line. Thescan_groupmethod is also weird - it starts a Task and then discards any reference to it, making it impossible to monitor it. Finally, the call toParallel.ForEachinside another task, just wastes tasks.