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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:01:55+00:00 2026-06-01T12:01:55+00:00

I am using the Task Parallel Library to set up a chain of Tasks

  • 0

I am using the Task Parallel Library to set up a chain of Tasks as shown below but I am getting an odd exception handling experience that I don’t understand.

I use Parallel.ForEach and invoke an Action which includes a call to the following method. This Parallel.ForEach is wrapped in a try…catch(AggregateException) and when an exception occurs – as it does in one of the Parallel branches – a SchemaValidation exception then I would expect to see that in the AggregateException.

However, what I get is ‘A task was canceled’ – TaskCanceledException. Where has my SchemaValidationException gone?

        private static void ProcessChunk(Task<ISelectedChunk> selectionTask, 
                                     IRepository repository, 
                                     IIdentifiedExtractChunk identifiedExtractChunk, 
                                     IBatchRunConfiguration batchRunConfiguration, 
                                     IBatchRun batchRun, 
                                     ILog log, 
                                     IAuthenticationCertificate authenticationCertificate, 
                                     IFileSystem fileSystem,
                                     long batchRunRid)
    {
        var transformationTask = selectionTask.ContinueWith(TransformationFunction.Transformation(identifiedExtractChunk, batchRunConfiguration, batchRun),
                                                            TaskContinuationOptions.NotOnFaulted);

        var schemaValidationTask = transformationTask.ContinueWith(SchemaValidationFunction.SchemaValidationTask(batchRunConfiguration),
                                                                   TaskContinuationOptions.NotOnFaulted);

        var compressTask = schemaValidationTask.ContinueWith(CompressFunction.CompressTask(identifiedExtractChunk),
                                                             TaskContinuationOptions.NotOnFaulted);

        var encryptTask = compressTask.ContinueWith(EncryptionFunction.EncryptTask(authenticationCertificate),
                                                    TaskContinuationOptions.NotOnFaulted);

        var fileGenerationTask = encryptTask.ContinueWith(FileGenerationFunction.FileGenerationTask(identifiedExtractChunk, batchRunConfiguration, fileSystem),
                                                          TaskContinuationOptions.NotOnFaulted);
        // Take the time before we start the processing
        DateTime startBatchItemProcessing = DateTime.Now;

        // Start with the Selection Task
        selectionTask.Start();

        // And wait on the last task in the chain
        fileGenerationTask.Wait();

        // Take the time at the end of the processing
        DateTime endBatchItemProcessing = DateTime.Now;

        // Record all the relevant information and add it to the collection 
        IBatchChunkProcessed batchChunkProcessed = GetBatchItemProcessed(identifiedExtractChunk, batchRunRid, fileGenerationTask.Result, transformationTask.Result.Item2, startBatchItemProcessing, endBatchItemProcessing);
        BatchItemsProcessed.Add(batchChunkProcessed);
  • 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-01T12:01:57+00:00Added an answer on June 1, 2026 at 12:01 pm

    Let’s simplify your code a bit:

    var t1 = Task.Factory.StartNew(a1);
    var t2 = t1.ContinueWith(a2, TaskContinuationOptions.NotOnFaulted);
    var t3 = t2.ContinueWith(a3, TaskContinuationOptions.NotOnFaulted);
    
    t3.Wait();
    

    Now assume that a1 throws an exception. What happens is that t1 becomes faulted (t1.Status == TaskStatus.Faulted). Because of that, t2 can’t run (because of NotOnFaulted) and so it will be canceled. But this is not what you presumably expected: t2 won’t be faulted, it will be canceled (t2.Status == TaskStatus.Canceled). But this means that t3 can run normally, and if it doesn’t throw, t3.Wait() won’t throw any exception.

    How to fix this? First, you probably shouldn’t use TaskContinuationOptions.NotOnFaulted, but TaskContinuationOptions.OnlyOnRanToCompletion instead. But that doesn’t solve the problem of “disappearing” exceptions. To solve that, I see two possibilities:

    1. Call Wait() at the beginning of each continuation and don’t use any TaskContinuationOptions. This means you might get some exception wrapped in AggregateException, which is itself wrapped in AggregateException, which is wrapped in another AggregateException, etc. To solve this, you could use Flatten() or Handle().

    2. Wait for all the tasks, by using Task.WaitAll(). WaitAll() will throw an AggregateException that will contain the original exception and also TaskCanceledException for each of the tasks that were canceled because of the first exception.

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

Sidebar

Related Questions

I'd like to start using the Task Parallel Library , as this is the
I'm using the TPL ( Task Parallel Library ) in .NET 4.0. I want
I have a long-running Task that I've implemented using the Task Parallel Library. When
Before using the Task Parallel Library, I have often used CorrelationManager.ActivityId to keep track
I'm using the .NET 4.0 Task Parallel Library with C# (my first time using
If I am using System.Threading.Task to run parallel tasks like this: Func<MyResult> func=ComputeResult; var
I am using the task parallel library from .NET framework 4 (specifically Parallel.For and
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 a quantifiable & repeatable problem using the Task Parallel Library, BlockingCollection<T> ,

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.