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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:51:10+00:00 2026-06-11T19:51:10+00:00

I am really really confused about Parallel.ForEach… How does it work? The code below

  • 0

I am really really confused about Parallel.ForEach… How does it work?
The code below has an error -> File Is In Use

Parallel.ForEach(list_lines_acc, (line_acc, list_lines_acc_state) =>
{
     FileStream file = 
         new FileStream(GPLfilePath, FileMode.Open, FileAccess.ReadWrite);
     StreamReader reader = new StreamReader(file);
     var processed = string.Empty;
     Ok_ip_port = string.Empty;
     while (reader.EndOfStream)
     {
         if (string.IsNullOrEmpty(Ok_ip_port))
         {
             Ok_ip_port = reader.ReadLine();
         }
         else
         {
             string currentLine = reader.ReadLine();
             processed += currentLine + Environment.NewLine;
         }
     }
     StreamWriter writer = new StreamWriter(file);
     writer.Write(processed);

     reader.Close();
     writer.Close();
     file.Close();
});  

Would you please show me how can I fix that? This code is just an example.

I want to work with string arrays & Lists inside Parallel.ForEach, but there is always a problem for adding or editing those collections. Can you please provide an example? I am using Visual Studio 2010 + .NET Framework 4.0

  • 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-11T19:51:11+00:00Added an answer on June 11, 2026 at 7:51 pm

    In your code, as written, each thread is using the same file, and effectively trying to append to it. Even if this could work, you would have a bad race condition (as the threads would be trying to append to the same file simultaneously).

    The error you’re seeing is purely because you’re using the same file in each loop iteration, so when you try to open the file (after the first iteration), it’s erroring out as it’s opened by a different loop iteration.

    Also, you’re never using your loop variable (line_acc), so there is really no need for a loop here at all. This could be written without the Parallel.ForEach, and you have the same result, with no issues.

    That being said – if this is example code, you’ll tend to find that loops that are bound purely by file I/O will tend to not parallelize well. The actual drive being used will become the limiting factor, so running code that purely reads and writes to a file in parallel will often cause the resulting code to run slower, not faster, than running it sequentially.

    I want to work with string arrays & Lists inside Parallel.ForEach, but there is always a problem for adding or editing those collections

    The code you’re showing “as an example” is doing none of this, so it’s difficult to see where your issue might be occurring. You can write to an array or List<T> by index, but you can’t add to a list in a parallel loop without extra synchronization (such as a lock), as List<T> is not thread safe for writes. If you are trying to read and write from collections, you might consider looking at the System.Collections.Concurrent namespace, which contains thread safe collections you can safely use in Parallel.ForEach loops.

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

Sidebar

Related Questions

Well I'm new to PHP but thinking about these stuff has really confused me
I'm really confused about this. I'm using the Ruby class below in an automated
http://php.about.com/od/finishedphp1/ss/php_login_code_4.htm I am really confused about three particular lines of code in this tutorial
Okay, I'm really confused about this error. I know in the past having a
I am really confused about when to use WCF and when to use ADO
I am really confused about End of File. Suppose I am running an infinite
I am really confused about classes in haskell. If I had the code: class
Working on someone else's code. I am really confused about whats exactly happening in
I am really confused about object relationships! I have two classes Person and Address.
I'm really confused about the visitor pattern and its uses. I can't really seem

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.