none
Deleted RRS feed

Answers

  • The reason why it doesn't work is that you open the StreamWriter inside the Parallel.Foreach. When you do this, every simultaneous thread attempts to open the same file for writing, which is not allowed. Only one StreamWriter can write to the file at any one time.

    The solution is to open the StreamWriter only once before entering the Parallel.ForEach. Then you can use the same StreamWriter from all the treads. Note that the StreamWriter is not thread-safe, so you need to enclose your Write statements inside a lock (...). If you'd prefer to use a thread-safe wrapper, search the documentation for TextWriter.Synchronized.

    Saturday, January 19, 2019 8:41 PM
    Moderator