I developed an application that saves 10000 lines of lorem ipsum in .txt file on the disk. I used StreamWriter to actually write these lines, and ThreadPool to distribute the computational process to the available threads/cores.
The saving is very fast, even with 1 million lines, but the problem is, not all lines are saved and the last one is saved partially. When I close the program, it writes to file again for some reason.
I have the following string:
String loremipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
And the rest of the code is:
private StreamWriter writer = new StreamWriter("loremipsum.txt");
private Object lockObject = new Object();
// Button that initiates the process
private void btnSave_Click(object sender, RoutedEventArgs e)
{
for (int i = 1; i <= 10000; i++)
{
ThreadPool.QueueUserWorkItem(state =>
{
SaveFile(writer, loremipsum);
});
}
}
private void SaveFile(StreamWriter w, String text)
{
lock (lockObject)
{
w.WriteLine(text);
}
}
Since the StreamWriter is not thread-safe, I'm using a locking object.
If I open my .txt file with my program running it has only 9998 lines with the last one being incomplete.
If I close the program, it rewrites to file again, adding all the lines, but again the last one is incomplete. Why this?
I suppose the lines are saved partially because the ThreadPool's threads need some time to finish their work. Am I correct? If so, how can I achieve that?
Threadpool is making Background threads to perform the tasks that will stop be aborted if the main thread is aborted. You have to wait for all threads to finish and then Flush the stream. As you are missing just last lines, More likely you only have a Flush() issue there,not aborted threads.
I trust you also know that your code is worse than doing the work in a single thread. There is no real parallelism happening there while all the overhead is created.