Let's say I have two independent asynchronous functions (which I do not control) that create Task objects:
Task A(); Task B();
and some other non-asynchronous function
void X();
How to create a whole chain of tasks that performs all these sequences and allows you to add additional continuations (which will be executed after X)?
If I do this:
Task Sequential() { return A() .ContinueWith(t => { B(); }) .ContinueWith(t => { X(); }); }
which does not work, because B will launch a new task chain. If B takes a long time, X will be executed first (along with what else the caller from SequentialWith can call on the returned Task). I need X to be the last element in the same task chain, task B as a precedent.
If I do this:
Task Sequential() { return A() .ContinueWith(t => { B().ContinueWith(t => { X(); }); }); }
which only partially solves the problem, because although A, B and X will now be executed in sequential order, if the caller executes Sequential (). Continue. Continuation will be performed in parallel with B and X, and not after X.
source share