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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:32:09+00:00 2026-05-26T09:32:09+00:00

When I run the following code piece, a IndexOutOfRangeException is thrown. It appears that

  • 0

When I run the following code piece, a IndexOutOfRangeException is thrown. It appears that i is 2 when the exception is thrown. My understanding is that the new thread is started after the value of i has been changed. Is there a way to make this code safe from this kind of problem?

int x[2] = {1, 3};
int numberOfThreads = 2;

for (int i = 0; i < numberOfThreads; i++)
{
    new Thread(() =>
    {
        DoWork(x[i]);
    }).Start();
}
  • 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-05-26T09:32:10+00:00Added an answer on May 26, 2026 at 9:32 am

    The problem is that the variable i is being captured, and by the time the thread actually gets to start, it’s 2.

    Use this instead:

    for (int i = 0; i < numberOfThreads; i++)
    {
        int value = x[i];
        new Thread(() => DoWork(value)).Start();
    }
    

    Or:

    foreach (int value in x)
    {
        int copy = value;
        new Thread(() => DoWork(copy)).Start();
    }
    

    Or:

    for (int i = 0; i < numberOfThreads; i++)
    {
        int copyOfI = i;
        new Thread(() => DoWork(x[copyOfI])).Start();
    }
    

    In each case, the lambda expression will capture a new variable on each iteration of the loop – a variable which won’t be changed by subsequent iterations.

    In general, you should avoid capturing the loop variable in a lambda expression which will be executed later. See Eric Lippert’s blog post on the topic for more details.

    As of C# 5, it’s likely that the foreach loop behaviour will be changed to avoid this being a problem – but the for loop equivalent would still be an issue.

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

Sidebar

Related Questions

I have the following piece of code that I'm expecting to run on a
When I run the following code, I get the exception below: ''# NOTE: ExcelApp
I have the following piece of code: TableViewer tv = new TableViewer(attributeTable); tv.setContentProvider(new BOAttributeTableContentProvider());
I have a piece of code that makes the Visual Studio 2008 IDE run
Here is the following piece of code that I am trying to execute on
I have the following piece of code: public class DumpLocationLog extends Thread { LocationManager
I'm working on an app that writes to excel. The following piece f code
I have the following piece of code which is being run from a click
When I run the following code to test copying a directory, I get a
I want to run the following code: ajaxUpdate(10); With a delay of 1 second

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.