using (var stream = new FileStream(
this.filePath,
FileMode.Append,
FileAccess.Write,
FileShare.ReadWrite))
{
using (var writer = new StreamWriter(stream))
{
writer.Write("A randomly generated string from process X thread Y");
writer.Flush();
stream.Flush();
}
}
I am basically using this piece of code in 2 test methods in 2 different Visual Studios (different solutions). I'm running 2 threads per test for 1000 calls to this.
However, when I execute this, all the lines from whichever test I executed first get written to the file, and THEN all the lines get written to the file by the other process. I thought that the writer.Flush()
and stream.Flush()
calls would prevent this from happening.
How can I write to a file simultaneously from 2 different processes? If I write to a file simultaneously from 2 different threads within the SAME process, the writing occurs as expected (lots of alternating between threads). But for 2 different processes, whichever one is started first does all of its writing, and then the other one goes. Why is this?