private void Write(byte[] Data) { // allow this only once at a time... lock (this) { if (AsynchronousMode) { if (MaximumAsyncBufferSize < Data.Length) { // if the Maximum Async Buffer Size if larger than the size of the data we have to write it synchronously // And to write it syncronously we have to make sure we do not write in the middle of the existing queue while (WriteThreadInstance.BytesInAsyncBuffer > 0) { Thread.Sleep(1); } // the buffer is empty now, write the new element _AppendLog.Write(Data); Data = null; } else { if (WriteThreadInstance.BytesInAsyncBuffer + Data.Length <= MaximumAsyncBufferSize) { // yeah, it will fit into the buffer WriteThreadInstance.Write(Data); } else { // obviously the buffer is filled - wait till there's room while (WriteThreadInstance.BytesInAsyncBuffer + Data.Length <= MaximumAsyncBufferSize) { Thread.Sleep(1); } WriteThreadInstance.Write(Data); } } } else { // Syncronous-Mode writes are easy _AppendLog.Write(Data); } } }
public void Run() { byte[] data = null; while (!Stop) { lock (Async_Buffer) { if (Async_Buffer.Count > 0) { data = Async_Buffer[0]; Async_Buffer.RemoveAt(0); BytesInAsyncBuffer = BytesInAsyncBuffer - data.Length; } } // finally write it... if (data != null) { internalAppendLog.Write(data); // write it data = null; // null it } else { Thread.Sleep(1); // wait at least 1 msec if nothing to do } } // handle shutdown event by writing everything in the buffer to disk... while (Async_Buffer.Count != 0) { data = Async_Buffer[0]; Async_Buffer.RemoveAt(0); BytesInAsyncBuffer = BytesInAsyncBuffer - data.Length; internalAppendLog.Write(data); // write it } data = null; ShutdownComplete = true; }