Scott Stafford :
I want to return a single await
-able Task from my method that launches several async executions. How can I do this?
public async Task DoThingsAsync() {
List<Task> tasks = new List<Task>();
tasks.Add(DoAsync());
tasks.Add(DoAsync());
tasks.Add(DoSomethingElseAsync());
return WHAT_CAN_GO_HERE(tasks);
}
Damien_The_Unbeliever :
Remove async
from your method and return the result of Task.WhenAll
:
public Task DoThingsAsync() {
List<Task> tasks = new List<Task>();
tasks.Add(DoAsync());
tasks.Add(DoAsync());
tasks.Add(DoSomethingElseAsync());
return Task.WhenAll(tasks);
}
If there are actually await
s hiding in your real code, you may instead stay async
and await Task.WhenAll(tasks);
inside this method. You then of course just return;
rather than returning anything, because that's what async Task
(not Task<T>
) methods do.
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=220091&siteId=1