Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7842847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:24:59+00:00 2026-06-02T16:24:59+00:00

How can I execute a System.Threading.Task as the continuation of two or more other

  • 0

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.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:25:00+00:00Added an answer on June 2, 2026 at 4:25 pm

    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));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I execute system command like dir other than xp_cmdshell ?
Talking about System.Collections.Generic.List<T> here. With example below can Method1 and Method2 execute and the
As we can execute such executables in two ways, such as sudo mono test.exe,
My goal is to have an AsyncTask that can execute multiple times (one task
I am using a System.Threading.Timer in Windows service to execute a method periodically. The
I am using System.Threading.Tasks to execute my long running service calls and using ContinueWith
how can i execute system calls within a trigger? my code currently looks like
In Perl, you can execute system commands using system() or `` (backticks). You can
Can I create an event so I can execute some javascript whenever an element
Is there anyway I can execute stored procedure via EXEC (so not specifying CommandType.StoredProcedure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.