C # Timed Buffer – Code Revision Stack Exchange

The purpose of this code is to have a buffer that will process batch messages based on the configured timeout. For example, a thread will continually push the data into this buffer and, once the configured delay time is set, it will trigger an event to process the data (such as batch processing and sending to a source external).

BufferReadyEventArgs.cs

Public class BufferReadyEventArgs : EventArgs
{
public BufferReadyEventArgs (List buffer)
{
Buffer = buffer ?? throws new ArgumentNullException (nameof (buffer));
}

public list Buffer {get; }
}

TimedBuffer.cs

Public class TimedBuffer
{
public event EventHandler <BufferReadyEventArgs> BufferReady;

private object read-only _bufferElapsedSyncRoot = new object ();
private object read-only _bufferSyncRoot = new object ();
private read-only Timer _processBufferTimer;
Volatile volatile list _buffer = new list();

public TimedBuffer (double intervalInMsecs)
{
_processBufferTimer = new Timer (intervalInMsecs);
_processBufferTimer.Elapsed + = _processBufferTimer_Elapsed;
_processBufferTimer.Enabled = true;
}

Private Void _processBufferTimer_Elapsed (Object Sender, ElapsedEventArgs e)
{
lock (_bufferElapsedSyncRoot)
{
listing readerList = _buffer;
lock (_bufferSyncRoot)
{
_buffer = new list();
}

BufferReady? .Invoke (this, new BufferReadyEventArgs(ReaderList));
}
}

public void Add (T element)
{
lock (_bufferSyncRoot)
{
_buffer.Add (item);
}
}
}

I would like to have a code review for:

  • General (like name, design, code style, etc.)
  • Performance (especially locking part)