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

  • Home
  • SEARCH
  • 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

I have code where I schedule a task using java.util.Timer . I was looking
I have an Ant script that performs a copy operation using the 'copy' task
I'm using DRb within a Rails application to offload an expensive task outside the
I have a task and I want to generate some code using the CodeDom.
I am using Ant's <parallel> task to perform multiple simultaneous targets that use <exec>
I noticed using task manager that the following code has a GDI leak in
I want to automate a Windows 2000+ server reboot process using Task Scheduler or
We are using the default Task list from a Team Site and would like
We've been using Trac for task/defect tracking and things were going well enough, but
I need to execute a http web request from Plesk's Task Scheduler (using shared

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.