简体   繁体   中英

C#, ConcurrentQueue size limit and time frame

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);
    }
}

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM