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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:39:56+00:00 2026-06-06T17:39:56+00:00

This is my first question in this forum, so i hope to be clear:

  • 0

This is my first question in this forum, so i hope to be clear:

I´m using Visual Studio 2010 RTM Programming in C#

The constructor of the class, which is converted to a thread, are always taking the values from the last Dictionary Entry.
I don´t know what i´m doing wrong, someone?

This Code doesn´t work:

    Dictionary<string, Queue<string>> colas;

    public DataDispatcher(Dictionary<string, int> lectoras, ReadEventHandler callback)
    {
        colas = new Dictionary<string, Queue<string>>();

        foreach (KeyValuePair<string, int> pair in lectoras)
        {
            colas.Add(pair.Key, new Queue<string>>());

            Thread hilo = new Thread(
                () => new ReadHandler(pair.Value, colas[pair.Key], callback));

            hilo.Name = "Hilo-" + hilo.ManagedThreadId.ToString();

            hilo.Start();
        }
    }

Instead, this Code works perfectly:

    Dictionary<string, Queue<string>> colas;

    public DataDispatcher(Dictionary<string, int> lectoras, ReadEventHandler callback)
    {
        colas = new Dictionary<string, Queue<string>>();

        foreach (KeyValuePair<string, int> pair in lectoras)
        {
            Queue<string> qs = new Queue<string>();

            colas.Add(pair.Key, qs);

            int jaula = pair.Value;

            Thread hilo = new Thread(
                () => new ReadHandler(jaula, qs, callback));

            hilo.Name = "Hilo-" + hilo.ManagedThreadId.ToString();

            hilo.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-06-06T17:39:59+00:00Added an answer on June 6, 2026 at 5:39 pm

    It is a very common error called ‘capturing the loop variable’, or ‘closing over the variable’.

     foreach (KeyValuePair<string, int> pair in lectoras)
     {
         colas.Add(pair.Key, new Queue<string>>());  // here 'pair' is OK
    
         var copy = pair;  // this is the fix, make a new 'copy' for each thread
    
         Thread hilo = new Thread(
             () => new ReadHandler(copy.Value, colas[copy.Key], callback)); 
     }
    

    You were starting a number of threads all using the same variable pair. This ‘captured’ variable behaves like a pass-by-reference parameter. By the time the Threads start executing the foreach() is done and they all use the last element.

    On a side note, you probably shouldn’t be using bare Threads for this. Use the ThreadPool or Task objects from the TPL.

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

Sidebar

Related Questions

This is my first question on Stack Overflow, so I hope that I'm clear
this is my first question in the forum, I hope you can help me
This is my first question to this forum. I am trying to make this
(Since this is my first SO question, let me just say I hope it's
It is the first time I ask a question on this forum. Sorry if
This is my first question here, but I hope I will get a lot
This is my first time posting a question to the forum, by the way.
I am new the forum so hope this question is not too elementary or
this is my first question in here, and I would like to ask if
This is my first question, so please be gentle. I'm trying out Apache Camel

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.