How are you opening the file? And how do you know that both threads won't write to the same file simultaneously, thereby overwriting the other's work? This is a classical shared resource problem, and it's typically solved by busy waiting. Perform each calculation in the thread, but only allow a thread to open the file if it exists and is closed. If it's not closed, sleep until it opens. Once it's open, immediately write the data and close the file.
If you can somehow guarantee exclusive access or know that they won't be overwriting each other, make sure that you're setting the FileShare parameter when you open the file.
i.e.
new FileStream("Filename.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
__________________
"I'm typing on a computer of science, which is being sent by science wires to a little science server where you can access it. I'm not typing on a computer of philosophy or religion or whatever other thing you think can be used to understand the universe because they're a poor substitute in the role of understanding the universe which exists independent from ourselves." - Willravel
|