title: How to correctly write to a file using Parallel.ForEach?
body: <p>I have a task which reads a large file line by line, does some logic with it, and returns a string I need to write to a file. The order of the output does not matter. However, when I try the code below, it stops/get really slow after reading 15-20k lines of my file. </p>
  
  <pre><code>public static Object FileLock = new Object();
  ...
  Parallel.ForEach(System.IO.File.ReadLines(inputFile), (line, _, lineNumber) =&gt;
  {
      var output = MyComplexMethodReturnsAString(line);
      lock (FileLock)
      {
          using (var file = System.IO.File.AppendText(outputFile))
          {
              file.WriteLine(output);
          }
      }
  });
  </code></pre>
  
  <p>Why is my program slow down after some time running? Is there a more correct way to perform this task? </p>