Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing tasks in parallel

Ok, so basically I have a bunch of tasks (10) and I want to start them all at the same time and wait for them to complete. When completed I want to execute other tasks. I read a bunch of resources about this but I can't get it right for my particular case...

Here is what I currently have (code has been simplified):

public async Task RunTasks() {     var tasks = new List<Task>     {         new Task(async () => await DoWork()),         //and so on with the other 9 similar tasks     }       Parallel.ForEach(tasks, task =>     {         task.Start();     });      Task.WhenAll(tasks).ContinueWith(done =>     {         //Run the other tasks     }); }  //This function perform some I/O operations public async Task DoWork() {     var results = await GetDataFromDatabaseAsync();     foreach (var result in results)     {         await ReadFromNetwork(result.Url);     } } 

So my problem is that when I'm waiting for tasks to complete with the WhenAll call, it tells me that all tasks are over even though none of them are completed. I tried adding Console.WriteLine in my foreach and when I have entered the continuation task, data keeps coming in from my previous Tasks that aren't really finished.

What am I doing wrong here?

like image 373
Yann Thibodeau Avatar asked Dec 19 '15 21:12

Yann Thibodeau


People also ask

What does it mean to do tasks in parallel?

Parallel tasks are split into subtasks that are assigned to multiple workers and then completed simultaneously. A worker system can carry out both parallel and concurrent tasks by working on multiple tasks at the same time while also breaking down each task into sub-tasks that are executed simultaneously.

Do async tasks run in parallel?

There is no parallelism here, as the “async Task” does not automatically make something run in in parallel. This will spawn 2 threads, run them simultaneously, and return when both threads are done. This will create a list of Tasks to be run at the same time.

Are tasks Parallel C#?

Task parallelism is the process of running tasks in parallel. Task parallelism divides tasks and allocates those tasks to separate threads for processing. It is based on unstructured parallelism.


1 Answers

You should almost never use the Task constructor directly. In your case that task only fires the actual task that you can't wait for.

You can simply call DoWork and get back a task, store it in a list and wait for all the tasks to complete. Meaning:

tasks.Add(DoWork()); // ... await Task.WhenAll(tasks); 

However, async methods run synchronously until the first await on an uncompleted task is reached. If you worry about that part taking too long then use Task.Run to offload it to another ThreadPool thread and then store that task in the list:

tasks.Add(Task.Run(() => DoWork())); // ... await Task.WhenAll(tasks); 
like image 110
i3arnon Avatar answered Sep 21 '22 16:09

i3arnon