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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:29:41+00:00 2026-06-17T11:29:41+00:00

I want to run async 4-tier nested loop and print all aggregate exception but

  • 0

I want to run async 4-tier nested loop and print all aggregate exception but fail to do so.
Followed MS post on how to handle exception in Parallel library.

Can get why I get always random number while it should print 3^3 times ?

Console.WriteLine("{0} was on counter\n\n\n",counter);

class Program
{
    static void Main()
    {
        int counter = 1;

        List<int> numbers = new List<int> {1, 2, 3};

        try
        {
            ConcurrentQueue<Exception> exceptions = new ConcurrentQueue<Exception>();
            Parallel.ForEach(numbers, number1 =>
            {
                Parallel.ForEach(numbers, number2 =>
                {
                    Parallel.ForEach(numbers, number3 =>
                    {
                        Parallel.ForEach(numbers, number4 =>
                        {
                            try
                            {
                                throw new Exception(string.Format("number {0} failed on iteration {1}",number4,counter++));
                            }
                            catch (Exception exception)
                            {
                                exceptions.Enqueue(exception);
                            }

                        });
                    });
                });
            });
            if (!exceptions.IsEmpty)
            {
                throw new AggregateException(exceptions);
            }
        }
        catch (Exception exception)
        {
            Console.WriteLine("{0} was on counter\n\n\n",counter);
            GetInnerException(exception);
        }

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }

    public static void GetInnerException(Exception exception)
    {
        if (exception.GetType().Equals(typeof(AggregateException)))
        {
            AggregateException aggregateException = (AggregateException)exception;
            foreach (Exception innerException in aggregateException.InnerExceptions)
            {
                GetInnerException(innerException);
            }
        }
        else
        {
            Console.WriteLine(exception.ToString());
        }
    }
}

thanks

  • 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-17T11:29:42+00:00Added an answer on June 17, 2026 at 11:29 am

    The counter variable is not incremented in a thread-safe fashion.

    As the TPL loops will run in parallel, your counter variable will be incremented from multiple threads. As incrementing an integer variable is not atomic, you’ll need to make this part thread-safe, for example by using a lock or Interlocked.Increment:

    throw new Exception(string.Format("number {0} failed on iteration {1}", 
                                      number4, 
                                      Interlocked.Increment(ref counter)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in a webapp i want to run an async thread for a long process
I want run all script from the directory . Like , The directory contains
how to run the async task at specific time? (I want to run it
Original problem I want to run queries on database from within Twisted async app.
I am using async.js to run a for loop over a collection. For performance
I am doing a simple Async operation with Android, but since I want to
I often want to run a movie but are only insterested in the debugging
I want to run async task in onReceive(Context context, Intent intent) method. now i
I want to run some code when an async task completes successfully. From reading
I want run a script as follows: runner: ssh 'java program &' ssh 'java

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.