currently i'm coding class that involves buffered file writing. scenario is pretty usual: you instantiate class, set properties, when needed call class methods. my class internally processes passed to methods string values and stores them in MemoryStream buffer. if buffer exceeds the specified value FlushBuffer() method is called, buffer is written to file. what i want to do is to run FlushBuffer() method in separate thread to not 'freeze' or slow-down operations that are running in the class caller thread since file io is relatively slow process. but this involves problems. if thread with FlushBuffer() method is running while the user calls class method that should process and store some string value in the buffer then some previously added but yet unsaved information might be overwritten. as i understand to avoid this i should use lock() statement on some member variables. i can't figure out on which ones since too many are involved in FlushBuffer() method. here is the code:
private void FlushBuffer(){
if (this.bufferedSize > 0)
{
FileStream fileToWrite= new FileStream(this.filename, FileMode.Append, FileAccess.Write, FileShare.Read);
BinaryWriter binWriter = new BinaryWriter(fileToWrite);
byte[] byteBuffer = new byte[this.bufferedSize]; if (this.bufferedSize < this.bufferSize)
{
this.memoryBuffer.Position = 0;
this.memoryBuffer.Read(byteBuffer, 0, this.bufferedSize);
}
else if (this.bufferedSize == this.bufferSize)
{
byteBuffer = this.memoryBuffer.GetBuffer();
}
binWriter.Write(byteBuffer);
binWriter =
fileToWrite.Flush();
fileToWrite.Close();
fileToWrite.Dispose();
fileToWrite= null;
this.memoryBuffer.Position = 0;
this.bufferedSize = 0;
}
}

advice needed on using lock()
Mystagogue
If you have not already done so, I'd suggest reading the MSDN reference material on the C# lock statement:
http://msdn2.microsoft.com/en-us/library/c5kehkcz.aspx
There is one part of that article that I will highlight here as well. You should generally not use the lock statement in any of the following ways:
lock(this)
lock(typeof(SomeType)) // Where "SomeType" is a publicly accessible type
lock("A string")
The article explains why these are problematic.
-Tom Meschter
Software Dev, Visual C# IDE
silentC
If i understand your code correctly, you store the information in the memoryBuffer variable.
so, you could do this
private void FlushBuffer()
{
byte[] byteBuffer;
lock(memoryBuffer){
if (this.bufferedSize > 0){
FileStream fileToWrite= new FileStream(this.filename, FileMode.Append, FileAccess.Write, FileShare.Read);
BinaryWriter binWriter = new BinaryWriter(fileToWrite);
byteBuffer = new byte[this.bufferedSize];
if (this.bufferedSize < this.bufferSize)
{
this.memoryBuffer.Position = 0;
this.memoryBuffer.Read(byteBuffer, 0, this.bufferedSize);
}
else if (this.bufferedSize == this.bufferSize)
{
byteBuffer = this.memoryBuffer.GetBuffer();
this.memoryBuffer.Position = 0;
this.bufferedSize = 0;
}
}
if (byteBufer == null){return;}
binWriter.Write(byteBuffer);
null;binWriter =
fileToWrite.Flush();
fileToWrite.Close();
fileToWrite.Dispose();
fileToWrite= null;
}
}
The thing you need to look out for is when adding some things to the buffer:
add them to the buffer first, then update the bufferedsize
Hope this helps, please mark the thread as finished if this answers your questions