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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:07:25+00:00 2026-06-07T08:07:25+00:00

In order to up performance on a WrapPanel, I have introduced threading to my

  • 0

In order to up performance on a WrapPanel, I have introduced threading to my WP7 app. There’s a long List of Item-objects in one list, that one by one is added to another `List. I have the following two lists:

public List<Item> OriginalItems;
public List<Item> CopyOfItems;

The logic that’s put inside the BackgroundWorker.DoWork event handler is as follows:

workerThread.DoWork += new DoWorkEventHandler((object sender, DoWorkEventArgs e) =>
{
    foreach (var item in OriginalItems)
    {
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            CopyOfItems.Add(item);
        });

        // I feel sooo sleepy
    }
});

Now, this works just fine when I replace my comment with Thread.Sleep(150) – but anything less (and every once in a while even with larger values) makes the code put in the same element several times in a row.

Why is this, and how can it be fixed?

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

    This is a known wrinkle in C# – and actually one which is being fixed in C# 5. When you capture the loop variable from a foreach loop in a lambda expression, you’re capturing one variable. That variable changes its value through the loop – so if you execute the delegate created from the lambda expression after the “original” iteration as completed, you’ll see the value from the “current” iteration instead.

    A simple work around is to declare and initialize a copy of the iteration variable in the loop, and capture that:

    foreach (var item in OriginalItems)
    {
        var copy = item;
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            CopyOfItems.Add(copy);
        });
    
        // I feel sooo sleepy
    }
    

    See Eric Lippert’s blog post “Closing over the loop variable considered harmful” for more details on this.

    Does your real code actually do any work in the loop, by the way? It’s not clear that you’re really using threading to do anything significant other than splitting the UI thread work into several chunks – which can be done without a BackgroundWorker.

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

Sidebar

Related Questions

Is there any difference in (asymptotic) performance between var a = Orders.OrderBy(order => order.Date).First()
I have a class that modifies data via some extension methods. In order to
In order to improve the performance of a query I have created a denormalized
I'm investigating some performance issues in our product. We have several threads (order of
May order of members in binary architecture of objects of a class somehow have
I have a web application running on JBoss 4.2.2. In order to monitor performance
I have an SQLite3 database which, in order to optimize performance, uses computed columns
In order to trace some performance issues, I'm trying to create a page that
I know that when joining across multiple tables, performance is dependent upon the order
I'm improving the performance of a HTML5 Game,while I reset some arrays,in order to

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.