Asp.net mvc5. asynchronous wait. waiting for a task with diff return types

I just started working with .net mvc 5 async. I have few tasks that I define at runtime for parallel work. They all have different types of data returned, and I want to use Task.WhenAll to wait for them. As discussed by question , but I don't have a predefined set of tasks to run. I need to create a set of tasks with different types of returned data at runtime and wait.

+4
source share
1 answer

You can simply create List<Task>and then use Task.WhenAll:

var tasks = new List<Task>();
var task1 = Func1Async();
tasks.Add(task1);
var task2 = Func2Async();
tasks.Add(task2);
...

await Task.WhenAll(tasks);

, , , :

var result1 = await task1;
var result2 = await task2;
+3

Source: https://habr.com/ru/post/1548072/


All Articles