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 7631053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:15:19+00:00 2026-05-31T06:15:19+00:00

I have a method with the following structure: public Task InitializeAsync() { var taskCompletionSource

  • 0

I have a method with the following structure:

public Task InitializeAsync()
{
    var taskCompletionSource = new TaskCompletionSource<bool>();

    Task firstTask = ...;

    // secondTask calls taskCompletionSource.TrySetResult(true) once it considers itself "done"
    Task secondTask = firstTask.ContinueWith(..., TaskContinuationOptions.OnlyOnRanToCompletion);

    Action<TasK> errorContinuation = x =>
        {
            taskCompletionSource.SetException(e.Exception);
        };

    firstTask.ContinueWith(errorContinuation, TaskContinuationOptions.OnlyOnFaulted);
    secondTask.ContinueWith(errorContinuation, TaskContinuationOptions.OnlyOnFaulted);

    return taskCompletionSource.Task;
}

Importantly:

  • the task returned by InitializeAsync isn’t considered complete until the secondTask decides so
  • secondTask only runs if firstTask is successful
  • failure of either firstTask or secondTask causes the overall task to fail

What I’m wondering is whether there’s a cleaner, simpler way to express this whilst achieving the same functionality. I’m using .NET 4.0 but am interested in whether 4.5 makes this easier, too.

  • 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-05-31T06:15:21+00:00Added an answer on May 31, 2026 at 6:15 am

    For .NET 4.0, I used an idea from this blog article to chain tasks like you describe. In particular, look at the section titled Then. Not that his version expects you to pass in a function that returns a task instead of just passing a method like you would to ContinueWith

    As an aside, Then gets you pretty close to the SelectMany you would need to be able to chain the tasks via LINQ from clauses. I mention this mostly as a syntax option until async/await in .NET 4.5, though I don’t actually use that myself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following inheritance structure: var environment:AvEnvironment = new AvEnvironment(...); addChild(environment); environment.addChild(new Terrain());
I have the following XML structure: <method constructor=true name=Main public=true> <parameterList/> <block> <call> <callAttrbute>
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I have the following method in my code: private bool GenerateZipFile(List<FileInfo> filesToArchive, DateTime archiveDate)
I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have the following method and interface: public object ProcessRules(List<IRule> rules) { foreach(IRule rule
I have the following method: public object[] GetEventsByUser(DateTime start, DateTime end, string fullUrl) The
I have a method which have this signature public static IList<T> GetBy<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression)
I have the following class structure: public class Fruit { } public class Apple
I have the following class structure: public class A : AInterface { } public

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.