1

i am looking for a way to : 1. read messages from a ConcurentQueue limited to some size. 2. read not more then X message at a time frame. i want to stop the reading from Q once one of the 2 hit, until other code is done and do the same thing again.

i saw different implementation for Queue spill over, in here Fixed size queue which automatically dequeues old values upon new enques but can t figure out how to combine them correctly.

public class FixedSizedQueue<T>

public int Size { get; private set; }

public FixedSizedQueue(int size)
{
    Size = size;
}

public void Enqueue(T obj)
{
    queue.Enqueue(obj);

    while (queue.Count > Size)
    {
        T outObj;
        queue.TryDequeue(out outObj);
    }
}

}

developer learn999
  • 197
  • 1
  • 2
  • 16

1 Answers1

0

Try to use BlockingCollection instead of ConcurrentQueue

https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.blockingcollection-1?view=netframework-4.8

When you try to add another element in a collection if is full it's wait until an element is taken.

L.Zoffoli
  • 99
  • 2
  • 9