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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:51:13+00:00 2026-06-14T08:51:13+00:00

I have the following code snippets: lock (lockObject) { foreach (KeyValuePair<string, List<string>> entry in

  • 0

I have the following code snippets:

lock (lockObject) 
{
  foreach (KeyValuePair<string, List<string>> entry in productDictionary) 
  {
    string writePath = String.Format(@"{0}\{1}-{2}.txt", directoryPath, hour,entry.Key);
    Task writeFileTask = Task.Factory.StartNew(() => WriteProductFile(writePath, entry.Value));
  }
}

productDictionary is a ConcurrentDictionary of <string, List<string>> which I am trying to iterate over. For each key value pair I am attempting to construct a file path based on the Key and then write out the list of strings stored in the Value. For this purpose I start up a new task that calls the following method:

public static void WriteProductFile(string filePath, List<string> productQuotes)
{
    using(StreamWriter streamWriter = new StreamWriter(filePath)) 
    {
        productQuotes.ForEach(x => streamWriter.WriteLine(x));
    }
}

Stepping through the code everything looks fine at first. Putting a breakpoint at the method call for WriteProductFile shows that the correct parameters are being passed into the method through the Task. However, when my program actually makes it down to the WriteProductFile method the parameters have become mismatched. Ie, a List of strings that does not match the filepath has been passed in and so my data is no good once the program completes. No errors are thrown and the program executes fine but the wrong information is being written out to the wrong files.

I thought that the ConcurrentDictionary and Lock would take care of any threading issues that would arise but apparently I missed something. Any ideas?

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

    You are capturing a loop variable. You must declare a local in inside the loop.

    foreach (KeyValuePair<string, List<string>> entry in productDictionary) 
    {
      string writePath = String.Format(@"{0}\{1}-{2}.txt", directoryPath, hour, entry.Key);
      List<string> list = entry.Value; // must add this.
      Task writeFileTask = Task.Factory.StartNew(() => WriteProductFile(writePath, list));
    }
    

    Prior to C# 5, a single loop variable is used for all iterations of the loop. Each closure references the same variable and therefore the value is updated when a new cycle of the loop starts. For a definitive explanation you should read Eric Lippert’s post: Closing over the loop variable considered harmfuil.

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

Sidebar

Related Questions

I have the following code snippets. protected IEnumerable<string> GetErrorsFromModelState() { var errors = ModelState.SelectMany(x
If we have the following 2 snippets of code in c++ that do the
Suppose I have the following code snippets, i.e. goo call foo, DataTable created by
I have run the following pair of code snippets in Chrome console, with the
I have the following code snippets var myCanvas = document.createElement(canvas); . . . var
I have the following code (snippets) in my website: The CSS: #something li{ display:block;
I have the following code snippets $(document).mousedown(function(event) { doSomething(); } I can capture the
So, I have the following snippets of code: Script.js- var menuSlider=function(){ var m,e,g,s,q,i; e=[];
Following this link I have a set of code snippets. But the snippet insertion
I have something analogous to the following code snippets: // file alpha.c void function_A()

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.