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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:11:33+00:00 2026-05-11T13:11:33+00:00

I was going to post a question, but figured it out ahead of time

  • 0

I was going to post a question, but figured it out ahead of time and decided to post the question and the answer – or at least my observations.

When using an anonymous delegate as the WaitCallback, where ThreadPool.QueueUserWorkItem is called in a foreach loop, it appears that the same one foreach-value is passed into each thread.

List< Thing > things = MyDb.GetTheThings(); foreach( Thing t in Things) {     localLogger.DebugFormat( 'About to queue thing [{0}].', t.Id );     ThreadPool.QueueUserWorkItem(         delegate()         {             try             {                 WorkWithOneThing( t );             }             finally             {                 Cleanup();                 localLogger.DebugFormat('Thing [{0}] has been queued and run by the delegate.', t.Id );              }         });  } 

For a collection of 16 Thing instances in Things I observed that each ‘Thing’ passed to WorkWithOneThing corresponded to the last item in the ‘things’ list.

I suspect this is because the delegate is accessing the ‘t’ outer variable. Note that I also experimented with passing the Thing as a parameter to the anonymous delegate, but the behavior remained incorrect.

When I re-factored the code to use a named WaitCallback method and passed the Thing ‘t’ to the method, voilà … the i’th instance of Things was correctly passed into WorkWithOneThing.

A lesson in parallelism I guess. I also imagine that the Parallel.For family addresses this, but that library was not an option for us at this point.

Hope this saves someone else some time.

Howard Hoffman

  • 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. 2026-05-11T13:11:34+00:00Added an answer on May 11, 2026 at 1:11 pm

    This is correct, and describes how C# captures outside variables inside closures. It’s not directly an issue about parallelism, but rather about anonymous methods and lambda expressions.

    This question discusses this language feature and its implications in detail.

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

Sidebar

Related Questions

I was going to post an answer on this other SO question but someone
I was going to post this on Database Administrators, but as it is slightly
I'm going to post the question and then the code that I currently have
Already checked this question but didn't answer. Background I have a fully functional RESTful
Go ahead and look to the bottom of this post for the actual question
I've only skimmed through, but I wanted to get my first question out here,
I'm sorry to post another question on this, but I just seem to be
I am going trough Facebook developer pages looking for right answer but all seems
So these 2 files I am going to post are each in my DataBase
This is going to be a long post and just for fun, so if

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.