Rob Lyndon
Rob Lyndon

Reputation: 12661

How do I perform WhenAll on an array of TaskAwaiters?

I am using an API that returns awaitable objects that aren't tasks (in fact, they are PendingResult instances from Google's Android API). I would like to await completion of all of them. But I can only await an array of tasks under the current model.

The CTP of the TPL had a TaskEx.WhenAll() extension, which you could use to await TaskAwaiter instances. But Task.WhenAll() only applies to Task instances.

How can I perform WhenAll on an array of TaskAwaiter instances?

Upvotes: 1

Views: 299

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456477

The CTP of the TPL had a TaskEx.WhenAll() extension, which you could use to await TaskAwaiter instances.

Are you sure? I don't remember that. Then again, it was a long time ago, so I may just not remember.

I am using an interface that returns an awaitable result that isn't a Task. An awaitable result [that] has a GetTaskAwaiter() method.

If it's returning a custom awaitable, then you can just use async/await to convert that to a Task:

async Task DoSomethingAsync(string parameter) => await NonTaskAsync(parameter);

And use Select as such:

string[] parameters = ...;
var tasks = parameters.Select(DoSomethingAsync);
await Task.WhenAll(tasks);

Upvotes: 6

Related Questions