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

  • Home
  • SEARCH
  • 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 7753943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:10:55+00:00 2026-06-01T12:10:55+00:00

The Code using System; using System.Threading; public delegate void LoadingProgressCallback(double PercentComplete,string ItemName); public delegate

  • 0

The Code

using System;
using System.Threading;

public delegate void LoadingProgressCallback(double PercentComplete,string ItemName);
public delegate void LoadCompleteCallback(int ItemID, string ItemName);

public static class Program
{
    public static void Main(string[] args)
    {
        LoadTest loadTest = new LoadTest();
        loadTest.LoadItems(args);
    }
}

public class LoadTest
{       
    ManualResetEvent resetEvent;
    int numThreads = 0;

    public LoadTest()
    {}

    public void LoadItems(string[] Items)
    {
        numThreads = 0;
        resetEvent = new ManualResetEvent(false);

        foreach(string item in Items)
        {
            Console.WriteLine("Adding {0} to ThreadPool",item);
            ThreadPool.QueueUserWorkItem
            (
                delegate
                {
                    Load(item, this.progCall, this.compCall);
                }
            );
            numThreads++;

            Thread.Sleep(100);//Remove this line

        }
        resetEvent.WaitOne();
    }

    public void progCall(double PercentComplete, string ItemName)
    {
        Console.WriteLine("{0}: is {1}% Complete [THREAD:{2}]",ItemName,PercentComplete.ToString(),Thread.CurrentThread.ManagedThreadId.ToString());
    }
    public void compCall(int ItemID, string ItemName)
    {
        Console.WriteLine("{0}: is Complete",ItemName);
        numThreads--;
        if(numThreads == 0)
        {
            resetEvent.Set();
        }
    }

    public void Load(string Item, LoadingProgressCallback progressCallback, LoadCompleteCallback completeCallback)
    {
        Console.WriteLine("Loading: {0} [THREAD:{1}]",Item,Thread.CurrentThread.ManagedThreadId.ToString());

        for(int i = 0; i <= 100; i++)
        {
            if(progressCallback != null)
            {
                progressCallback((double)i, Item);
            }
            Thread.Sleep(100);
        }
        if(completeCallback != null)
        {
            completeCallback(0,Item);
        }
    }
}

Observation

If I run this program from the command line, like this…

>TheProgram item1 item2

The output will look like this.

Adding item1 to ThreadPool
Loading: item1 [THREAD:3]
item1: is 0%
Complete [THREAD:3]
Adding item2 to ThreadPool
Loading: item2 [THREAD:4]
item2: is 0% Complete [THREAD:4]
item1: is 1% Complete [THREAD:3]
item2: is 1% Complete [THREAD:4]
item1: is 2% Complete [THREAD:3]
item2: is 2% Complete [THREAD:4]

However, if I remove this line.

Thread.Sleep(100);//Remove this line

From the LoadItems method, the output looks like this.

Adding item1 to ThreadPool
Adding item2 to ThreadPool
Loading: item2 [THREAD:4]
Loading: item2 [THREAD:3]
item2: is 0% Complete [THREAD:4]
item2: is 0% Complete [THREAD:3]
item2: is 1% Complete [THREAD:4]
item2: is 1% Complete [THREAD:3]
item2: is 2% Complete [THREAD:3]
item2: is 2% Complete [THREAD:4]

The Question

It seems as though two threads are being used, though they both seem to be acting on the same data. Why does the code behave this way?

  • 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-01T12:10:57+00:00Added an answer on June 1, 2026 at 12:10 pm

    You’re closing over the loop variable, which gives you an unexpected result. Try this instead:

    foreach(string item in Items)
    {
        string item2 = item;
        Console.WriteLine("Adding {0} to ThreadPool", item2);
        ThreadPool.QueueUserWorkItem
        (
            delegate
            {
                Load(item2, this.progCall, this.compCall);
            }
        );
        numThreads++;
    
        Thread.Sleep(100);//Remove this line
    
    }
    

    References

    • Closing over the Loop Variable in C#
    • Closing over the loop variable considered harmful
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code using System.Threading; class Test { volatile int counter = 0; public
Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {
I have the following code: using System; using System.Linq; using System.Linq.Expressions; public class Program
The winform: The code: using System; using System.Windows.Forms; namespace DemoApp { public partial class
Consider the following code: using System; namespace ConsoleApplication2 { class Program { static void
This code using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using System.Data.Entity.ModelConfiguration;
I have this code :- using (System.Security.Cryptography.SHA256 sha2 = new System.Security.Cryptography.SHA256Managed()) { .. }
I have the following code: using System; using System.Diagnostics; using System.IO; using PdfSharp.Pdf.Printing; namespace
here is a code : using System; using Nemerle.Collections; using Nemerle.Text; //using Nemerle.Utility; using
i have the following code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using

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.