How can I complete the System.Threading.Task task as a continuation of two or more other task results?
public Task<FinalResult> RunStepsAsync() { Task<Step1Result> task1 = Task<Step1Result>.Factory.StartNew(Step1); // Use the result of step 1 in steps 2A and 2B Task<Step2AResult> task2A = task1.ContinueWith(t1 => Step2A(t1.Result)); Task<Step2BResult> task2B = task1.ContinueWith(t1 => Step2B(t1.Result)); // Now merge the results of steps 2A and 2B in step 3 Task<FinalResult> task3 = task2A .ContinueWith( t2A => task2B.ContinueWith( t2B => Step3(t2A.Result, t2B.Result))) .Unwrap(); return task3; }
This works, but double ContinueWith seems inefficient. Is there a better way to do this, possibly using TaskCompletionSource? (I do not want to use locks or Task.WaitAll.)
Joel V. Earnest-DeYoung
source share