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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:42:52+00:00 2026-06-01T02:42:52+00:00

All, there are many question on the above topic but I believe this is

  • 0

All, there are many question on the above topic but I believe this is sufficiently different to warrant a new question. I have the following Task and a continuation to deal with a variety of task Status; TaskStatus.RanToCompletion, TaskStatus.Canceled and of course the AggregateException via TaskStatus.Faulted. The code looks like

Task<bool> asyncTask = Task.Factory.StartNew<bool>(() =>
    asyncMethod(uiScheduler, token, someBoolean), token);

asyncTask.ContinueWith(task =>
{
    // Check task status.
    switch (task.Status)
    {
        // Handle any exceptions to prevent UnobservedTaskException.             
        case TaskStatus.RanToCompletion:
            if (asyncTask.Result)
            {
                // Do stuff...
            }
            break;
        case TaskStatus.Faulted:
            if (task.Exception != null)
                mainForm.progressRightLabelText = task.Exception.InnerException.Message;
            else
                mainForm.progressRightLabelText = "Operation failed!";
        default:
            break;
    }
}

This all works well, but I am concerned whether or not I am doing this right, as there is the possibility of an AggregateException being thrown from within the continuation – what then?

I do not want to Wait on my asyncTask nor the continuation as this will block the return to the UI Thread. To catch any exceptions thrown from within a continuation can’t mean I have to do something like this surely

Task parentTask = Task.Factory.startNew(() => 
    {
        Task<bool> asyncTask = Task.Factory.StartNew<bool>(() =>
            asyncMethod(uiScheduler, token, someBoolean), token);

        Task continueTask = asyncTask.ContinueWith(task =>
            {
                // My continuation stuff...   
            }

        try
        {
            continueTask.Wait();
        }
        catch(AggregateException aggEx)
        { 
            // Some handling here...
        }
    });

would this even work? What is best practice here?

As always, thanks for your time.

  • 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-01T02:42:54+00:00Added an answer on June 1, 2026 at 2:42 am

    You can use traditional try/catch within your delegates watching for AggregateException or you can chain on specific continuations that will only ever run if the antecedent has faulted using the TaskContinuationOptions.OnlyOnFaulted option. The latter approach allows for very clean task workflows to be defined. For example:

    Task myRootTask = ....;
    
    myRootTask.ContinueWith(rootAntecdent =>
    {
        // this will only be executed if the antecedent completed successfully, no need to check for faults
    },
    TaskContinuationOptions.OnlyOnRanToCompletion);
    
    myRootTask.ContinueWith(rootAntecedent =>
    {
        // this will only be executed if the antecedent faulted, observe exception and handle accordingly
    },
    TaskContinuationOptions.OnlyOnFaulted);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, there are many question on this subject but none solve my problem. I
First of all there is probably a question like this already but i couldn't
I have seen many questions about the above topics but none that address this.
I have seen this question asked all over the internet and answered in many
There are many things that all programmers should know, but I am particularly interested
I have a sql file generated by mysqldump --all-databases . There are many databases
There are many variants on this kind of question. However I am specifically after
I have asked this question in many sites and my question remain unanswered. Some
There have been many times on StackOverflow where a user asks a question like
First of all: I do know that there are already many questions and answers

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.