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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:33:30+00:00 2026-06-02T04:33:30+00:00

Scenario: An asynchronous task in a loop executes a method containing arguments that change

  • 0

Scenario: An asynchronous task in a loop executes a method containing arguments that change as the program continues:

while(this._variable < 100)
{
    this._variable++; 
    var aTask = Task.Factory.StartNew(() =>
    {
        aList.add(this._variable);
        update(this._savePoint);
    });
}

If the loop runs faster than the tasks complete, will the list add the current value of the variable or is the variable saved locally and the original value added?

  • 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-02T04:33:32+00:00Added an answer on June 2, 2026 at 4:33 am

    Closures close over variables, not values. Therefore, incrementing _variable can alter the behavior of the task that refers to it.

    You can prevent this by making a local copy:

    while (this._variable < 100)
    {
        this._variable++;
        int local = _variable;
        var aTask = Task.Factory.StartNew(() =>
        {
            aList.add(local);
            update(this._savePoint);
        });
    } 
    

    Or you could pass the value to the task as state:

    while (this._variable < 100)
    {
        this._variable++;
        var aTask = Task.Factory.StartNew(object state =>
        {
            aList.add((int)state);
            update(this._savePoint);
        }, this._variable);
    } 
    

    These both work by copying the value of _variable to a new temporary variable. In the first case the local variable is defined inside the scope of the loop, so you get a new one for every iteration. In the second case, you make a copy of the value of _variable when you pass it to the Task as the state argument. If _variable were a reference type, these solutions wouldn’t work; you’d have to perform a clone.

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

Sidebar

Related Questions

I have the following Android scenario: my activity launches an asynchronous task to perform
In a Windows Form window, multiple events can trigger an asynchronous method. This method
We have an asynchronous task that performs a potentially long-running calculation for an object.
I have unmanaged code that calls an asynchronous managed method that returns a handle,
How to ensure that the exception thrown by @Asynchronous method from EJB 3.1 methods
Scenario: I want to have an enum containing all the playing cards in a
Scenario: You have an ASP.Net webpage that should display the next image in a
I am assigned to a performance-tuning-debugging-troubleshooting task. Scenario: a multi-application environment running on several
The scenario is RPC over message queues - since the underlying mechanism is asynchronous,
Can anyone give me an example scenario where Asynchronous Callback should be used in

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.