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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:30:34+00:00 2026-05-22T18:30:34+00:00

I am using the Task Parallel Library in my application. I have a Task

  • 0

I am using the Task Parallel Library in my application. I have a Task (let’s call it “DoSomething”) which may be canceled. Whether the task is faulted, canceled, or completes successfully, I have a continuation attached to that task which performs some cleanup.

In the code that launches this task, I want to return a Task object whose status (faulted, canceled, ran to completion) reflects the status of the DoSomething task, however it’s important that this task I return not reflect this status until the continuation task executes.

Here’s an example:

public Task Start(CancellationToken token)
{
    var doSomethingTask = Task.Factory.StartNew(DoSomething
                                                , token);

    var continuationTask = doSomethingTask.ContinueWith
                (
                 (antecedent) =>
                     {
                         if (antecedent.IsFaulted || antecedent.IsCanceled)
                         {
                             //Do failure-specific cleanup
                         }

   //Do general cleanup without regard to failure or success
                      }
                 );

//TODO: How do I return a Task obj which Status reflect the status of doSomethingTask,
//but will not transition to that status until continuationTask completes?
}

I could use a TaskCompletionSource, but that seems kludgy. Any other ideas?

  • 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-22T18:30:34+00:00Added an answer on May 22, 2026 at 6:30 pm

    I think that TaskCompletionSource is actually ideal for this scenario. I.e. you are trying to return a Task as a signal of completion of your work, but manually control when and how that task reports its status. You could easily hide the boiler plate required for this with an extension method like this:

    public static Task<T> WithCleanup<T>(this Task<T> t, Action<Task<T>> cleanup) {
        var cleanupTask = t.ContinueWith(cleanup);
        var completion = new TaskCompletionSource<T>();
        cleanupTask.ContinueWith(_ => {
            if(t.IsCanceled) {
                completion.SetCanceled();
            } else if(t.IsFaulted) {
                completion.SetException(t.Exception);
            } else {
                completion.SetResult(t.Result);
            }
        });
        return completion.Task;
    }
    

    and call it like this:

    var doSomethingTask = Task.Factory
      .StartNew<object>(DoSomething, token)
      .WithCleanup(Cleanup);
    

    The only real caveat is that you can’t do this with plain old Task since there is no non-generic TaskCompletionSource.

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

Sidebar

Related Questions

Before using the Task Parallel Library, I have often used CorrelationManager.ActivityId to keep track
I have a task to perform an HttpWebRequest using Task<WebResponse>.Factory.FromAsync(req.BeginGetRespone, req.EndGetResponse) which can obviously
I'm using the TPL ( Task Parallel Library ) in .NET 4.0. I want
I'd like to start using the Task Parallel Library , as this is the
I'm using the .NET 4.0 Task Parallel Library with C# (my first time using
I am using the task parallel library from .NET framework 4 (specifically Parallel.For and
I have been following the development of the .NET Task Parallel Library (TPL) with
I'm using Task Parallel Library (TPL ) for calculating Fibonacci number. Program is given
I'm using the .NET 4.0's Task Parallel Library for executing a long running task.
I have submitted a task using executors and I need it to stop after

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.