mskeel
Well-known member
Im having a hard time finding documentation to figure out how read/write locks work in .Net. The basic gist is that I have a log file that is constantly being written to and I want to be able to read the log file and determine meaningful statistics without having to stop the process that is writing the file. It is my understanding that a file can have many readers but only one writer. Does that hold true for .Net?
Im pretty sure the standard StreamWriter and StreamReader both get exclusive locks for reading and writing which means I cant use either of those to write or read because it will cause one application to barf depending on who gets the write lock first. I think that means I have to use a regular FileStream to read and write the file so I can have more fine grained control over what locks are in place.
The problem is I cant figure out how to set it all up. Heres what I am currently trying.
I cant seem to get my file locking right to allow this to happen. Any help would be greatly appreciated.
Im pretty sure the standard StreamWriter and StreamReader both get exclusive locks for reading and writing which means I cant use either of those to write or read because it will cause one application to barf depending on who gets the write lock first. I think that means I have to use a regular FileStream to read and write the file so I can have more fine grained control over what locks are in place.
The problem is I cant figure out how to set it all up. Heres what I am currently trying.
Code:
Dim writer as FileStream = File.Open(pathToFile, FileMode.Open, FileAccess.ReadWrite)
writer.Seek(0, SeekOrigin.End) I want to append but it looks like FileMode.Append makes the file exclusive
...
Dim reader As FileStream = File.Open(pathToFile), FileMode.Open, FileAccess.Read) Throws an exception: File being used by another process.
Ill shut everything down later...
I cant seem to get my file locking right to allow this to happen. Any help would be greatly appreciated.