In a scenario where await may be called on an 'empty' list of tasks.
How do I await a list of Task<T>
, and then add new tasks to the awaiting list until one fails or completes.
I am sure there is must be an Awaiter
or CancellationTokenSource
solution for this problem.
public class LinkerThingBob
{
private List<Task> ofmyactions = new List<Task>();
public void LinkTo<T>(BufferBlock<T> messages) where T : class
{
var action = new ActionBlock<IMsg>(_ => this.Tx(messages, _));
// this would not actually work, because the WhenAny
// will not include subsequent actions.
ofmyactions.Add(action.Completion);
// link the new action block.
this._inboundMessageBuffer.LinkTo(block);
}
// used to catch exceptions since these blocks typically don't end.
public async Task CompletionAsync()
{
// how do i make the awaiting thread add a new action
// to the list of waiting tasks without interrupting it
// or graciously interrupting it to let it know there's one more
// more importantly, this CompletionAsync might actually be called
// before the first action is added to the list, so I actually need
// WhenAny(INFINITE + ofmyactions)
await Task.WhenAny(ofmyactions);
}
}
My problem is that I need a mechanism where I can add each of the action
instances created above to a Task<T>
that will complete when there is an exception.
I am not sure how best to explain this but:
The task must not complete until at least one call to
LinkTo<T>
has been made, so I need to start with an infinite taskeach time
LinkTo<T>
is called, the new action must be added to the list of tasks, which may already be awaited on in another thread.