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 Task
s that aren't really finished.
What am I doing wrong here?
question from:
https://stackoverflow.com/questions/34375696/executing-tasks-in-parallel 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…