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
You aren't going to be able to concurrently write to the same writer from different threads. The object wasn't designed to support concurrent access.
Beyond that, the general idea of writing to the same file from multiple threads is flawed. You still only have one physical disk, and it can only spin so fast. Telling it to do things faster won't make it spin any faster.
Beyond that, you're not closing the writer, as you said, and as a result, the buffer isn't being flushed.
You also have a bug in that your anonymous method is closing over line
, and all of the methods are closing over the same variable, which is changing. It's important that they each close over their own identifier that won't change. (This can be accomplished simply by declaring line
inside of the while
loop.) But since you shouldn't be using multiple threads to begin with, there's no real need to focus on this.
You can also use File.ReadLines
and File.WriteAllLines
to do your file IO; it results in much cleaner code:
var values = File.ReadLines(inputFile)
.Select(line => line.Split(';')[5]);
File.WriteAllLines(outputFile, values);
If you were to want to parallelize this process it would be because you're doing some CPU bound work on each item after you read the line and before you write the line. Parallelizing the actual file IO, as said before, is likely to be harmful, not helpful. In this case the CPU bound work is just splitting the line and grabbing one value, and that's likely to be amazingly fast compared to the file IO. If you needed to, for example, hit the database or do some expensive processing on each line, then you would consider parallelizing just that part of the work, while synchronizing the file IO through a single thread.
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