Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I figured out the problem!</p> <p>The thread synchronization works as it should and so does the TextWriter.Synchronized() so the problem isn't really the threads at all. Take this into account:</p> <p>I create 3 instances of the Log class and point them all to "test.txt"</p> <pre><code>Log log1 = new Log(); Log log2 = new Log(); Log log3 = new Log(); log1.Open("test.txt"); //new file handle as instance member log2.Open("test.txt"); //new file handle as instance member log3.Open("test.txt"); //new file handle as instance member </code></pre> <p>In each call to Open() I am opening a new file handle to the same file, so I have 3 unique and separate file handles. Each file handle or Stream has it's own file pointer which seeks along the stream as I read or write.</p> <p>So, if we have the following:</p> <pre><code>log1.WriteLine("this is some text"); //handled on thread 1 log2.WriteLine("testing"); //handled on thread 2 </code></pre> <p>If Thread 1 starts to write to the file and completes the file contents will be</p> <p><em>this is some text</em></p> <p>When Thread 2 starts to write, because the file handles and streams are unique the current location of log1's file pointer is at 16 and log2's is still at 0 so after log2 is done writing, the resulting log file will read:</p> <p><strong>testing</strong> <em>some text</em></p> <p>So, all I need to do is make sure I open only 1 unique FileStream per log file and do the synchronization like I have been. Works great now!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload