How to merge two Task results in a third task?

How can I execute a System.Threading.Task as the 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 the double ContinueWith seems inefficient. Is there a better way to do this, perhaps with a TaskCompletionSource? (I don't want to use locks or Task.WaitAll.)

Answers


Use TaskFactory.ContinueWhenAll.

class Step1Result {}
class Step2AResult
{
    public Step2AResult(Step1Result result) {}
}
class Step2BResult
{
    public Step2BResult(Step1Result result) {}
}
class FinalResult 
{
    public FinalResult(Step2AResult step2AResult, Step2BResult step2BResult) {}
}

    public Task<FinalResult> RunStepsAsync()
    {
        var task1 = Task<Step1Result>.Factory.StartNew(() => new Step1Result());

        // Use the result of step 1 in steps 2A and 2B
        var task2A = task1.ContinueWith(t1 => new Step2AResult(t1.Result));
        var task2B = task1.ContinueWith(t1 => new Step2BResult(t1.Result));

        // Now merge the results of steps 2A and 2B in step 3
        return Task <FinalResult>
            .Factory
            .ContinueWhenAll(new Task[] { task2A, task2B }, tasks => new FinalResult(task2A.Result, task2B.Result));
    }

Need Your Help

How to extend a jquery ui widget ? (1.7)

javascript jquery jquery-ui

I would like to create a custom version of the sortable widget. I have been searching for documentation, but could not find something really accurate. The best information I found was : http://jque...

Algorithm for neatly indenting SQL statements (Python implementation would be nice)

python sql coding-style indentation pretty-print

I'd like to reformat some SQL statements that are a single string with newlines in to something that's much easier to read.