I would like to ask help on my code. I am a newbie and wanted to implement safe multi threading in writing to a text file.
StreamWriter sw = new StreamW
A StreamWriter
is simply not thread-safe; you would need to synchronize access to this via lock
or similar. However, I would advise rethinking your strategy generally:
Task
or the ThreadPool
might be fine, though) - a low number of threads perhaps separately dequeuing from a thread-safe queue would be preferable