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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:44:43+00:00 2026-06-11T18:44:43+00:00

This is from Joseph Albahari’s excellent C# 5.0 in a Nutshell book In one

  • 0

This is from Joseph Albahari’s excellent C# 5.0 in a Nutshell book

In one of his chapters, he mentions a race-condition in this code block ..my guess is it’s meant to be pretty self-evident, as he didn’t bother to specify where it was but running the code multiple times I was unable to produce the said-race condition

_button.Click += (sender, args) =>
{
   _button.IsEnabled = false;
   Task.Run (() => Go());
};

void Go()
{
    for (int i = 1; i < 5; i++)
    {
        int result = GetPrimesCount (i * 1000000, 1000000);
        Dispatcher.BeginInvoke (new Action (() =>
       _results.Text += result + " primes between " + (i*1000000) + " and " + 
                                   ((i+1)*1000000-1) + Environment.NewLine));
    }

    Dispatcher.BeginInvoke (new Action (() => _button.IsEnabled = true));
}
  • 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-11T18:44:45+00:00Added an answer on June 11, 2026 at 6:44 pm

    I don’t agree with @Serge’s answer. You don’t even need multiple threads to see the problem. Try to run your code in its original form and notice the output. For me it’s the following and is sometimes random (I fixed the first value):

    1000000 primes between 5000000 and 5999999
    1000000 primes between 5000000 and 5999999
    1000000 primes between 5000000 and 5999999
    1000000 primes between 5000000 and 5999999
    

    Notice the last 2 values. They’re all the same, but they should depend on i. The problem is not that the operation is not atomic, because the GUI thread will execute the actions sequentially anyway.

    The reason for this occurring is that the lambda function passed to BeginInvoke takes the value of i at the moment of execution, not at the moment of initialization, so they will all see the last value of i by the time they get executed. The solution is to explicitly pass i as a parameter to the lambda like so:

    for (int i = 1; i < 5; i++)
    {
        int result = 1000000;
        Dispatcher.BeginInvoke(new Action<int>(j =>
        results.Text += result + " primes between " + (j * 1000000) + " and " +
                                  ((j + 1) * 1000000 - 1) + Environment.NewLine), i);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I grabbed this from the book I'm learning from. public void run() { //
Im doing this from my phone so feel free to edit the code for
I have found useful regex expressions from the site, but this particular one eludes
I was reading C# 4.0 in a Nutshell by Joseph and Ben Albahari and
With help from @Joseph I have managed to create this script that works exactly
From this tutorial . see i have one pre-built static library named as stackoverflow.a
This (from body of a stored proc) is throwing a syntax error: IF (name
I've been trying this from a long time. I'm having today's date and I
I got this from Load web browser with web response . and am wondering
I'm cross-posting this from the wordpress section, b/c I think the problem lies more

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.