You just need to
await
the task in the continuation methods.
You'll probably also want to wait for all of the continuations to finish in your
MainMethod
.
public async Task ReactToDoSomething(Task<int> task)
{
int result = await task;
}
public async Task MainMethod()
{
Task<int> doSomething = DoSomething();
Task<int> doSomethingElse = DoSomethingElse();
Task<int> doSomethingDifferent = DoSomethingDifferent();
Task continuation1 = ReactToDoSomething(doSomething);
Task continuation2 = ReactToDoSomethingElse(doSomethingElse);
Task continuation3 = ReactToDoSomethingDifferent(doSomethingDifferent);
await Task.WhenAll(continuation1, continuation2, continuation3);
}
EDIT: As discussed in the comments, if you don't want to change the original "ReactTo" methods, you'll need a wrapper method for the continuation:
private async Task ReactTo<TResult>(Task<TResult> task, Action<TResult> react)
{
TResult value = await task;
react(value);
}
public async Task MainMethod()
{
Task<int> doSomething = DoSomething();
Task<int> doSomethingElse = DoSomethingElse();
Task<int> doSomethingDifferent = DoSomethingDifferent();
Task continuation1 = ReactTo(doSomething, ReactToDoSomething);
Task continuation2 = ReactTo(doSomethingElse, ReactToDoSomethingElse);
Task continuation3 = ReactTo(doSomethingDifferent, ReactToDoSomethingDifferent);
await Task.WhenAll(continuation1, continuation2, continuation3);
}