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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:21:20+00:00 2026-06-17T08:21:20+00:00

I have a situation to process the set of data in parallel, in the

  • 0

I have a situation to process the set of data in parallel, in the end I want to know how many of them in total have been processed successfully. I come with following dummy code by following the sample at http://msdn.microsoft.com/en-us/library/dd460703.aspx and http://reedcopsey.com/2010/01/22/parallelism-in-net-part-4-imperative-data-parallelism-aggregation/

    public void DoWork2()
    {
        int sum = 0;
        Parallel.For<int>(0, 10,
            () => 0,
            (i, lockState, localState) =>
            {
                DummyEntity entity = DoWork3(i);
                if (entity != null)
                {
                    Console.WriteLine("Processed {0}, sum need to be increased by 1.", i);
                    return 1;
                }
                else
                {
                    Console.WriteLine("Processed {0}, sum need to be increased by 0.", i);
                    return 0;
                }
            },
            localState =>
            {
                lock (syncRoot)
                {
                    Console.WriteLine("Increase sum {0} by {1}", sum, localState);
                    sum += localState;
                }
            }
            );
        Console.WriteLine("Total items {0}", sum);
    }

    private DummyEntity DoWork3(int i)
    {
        if (i % 2 == 0)
        {
            return new DummyEntity();
        }
        else
        {
            return null;
        }
    }

However the result changes every time I run. I think there is some thing wrong with the code. But could not figure out why.

  • 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-17T08:21:21+00:00Added an answer on June 17, 2026 at 8:21 am

    Your problem is your choice in overloads. You’ve stored local state information to minimize the use of global state, yet you’re not using the local state.

    If you note from the example you gave they use the subtotal (what you’ve called localState) in the body of the loop:

    subtotal += nums[j];
    return subtotal;
    

    Compare this to your code (made a bit more concise):

    if (entity != null)
    {
        return 1;
    }
    else
    {
        return 0;
    }
    

    No mention of localState is there, so you’ve effectively thrown away some of the answers. If you change it instead to read:

    if (entity != null)
    {
        return localState + 1;
    }
    else
    {
        return localState;
    }
    

    You’ll find the following answer on the command line (for this given problem):

    Total items 5
    

    This usage of local state is in order to reduce access to shared state.

    Here is a snippet from using 0..50 as the range:

    Processed 22, sum need to be increased by 1.
    Processed 23, sum need to be increased by 0.
    Increase sum 0 by 1
    Processed 8, sum need to be increased by 1.
    Processed 9, sum need to be increased by 0.
    Processed 10, sum need to be increased by 1.
    Processed 11, sum need to be increased by 0.
    Increase sum 1 by 2
    Increase sum 3 by 8
    Increase sum 11 by 10
    Processed 16, sum need to be increased by 1.
    Processed 17, sum need to be increased by 0.
    Processed 18, sum need to be increased by 1.
    Increase sum 21 by 4
    Total items 25
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have situation where a user can manipulate a large set of data (presented
I have a situation where I need to set my process' locale to en-US.
Working with a relatively large data set I have a situation where I need
I have a situation where user requests to do a long running process. I
I have a set of rewrite rules that are supposed to process a URL
i have a situation, i need to process an jagged array of 20k registers
I have a situation where I'm starting a process in my code in order
I am using a commonj.work.WorkManager to process a few queries in parallel. I have
I have the following situation: I have set up a series of Cron jobs
A situation I have under Windows XP (SP3) has been driving me nuts, and

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.