Is there a .NET queue class that allows for dequeuing multiple items at once?
You can achieve this in .NET with Linq by using the Enumerable.Range()
method along with Select()
extension method:
var chunk = Enumerable.Range(0, chuckCount).Select(i => queue.Dequeue()).ToList();
This works by generating an enumerable of integers then for each integer in the new enumerable it dequeues an item out of your queue. Ensure the operation is done immediately by invoking ToList()
.
TPL Dataflow library offers BatchBlock < T > that groups input sequence of messages into chunks of desired size.
var bb = new BatchBlock<int>(10);
var ab = new ActionBlock<int[]>((Action<int[]>)chunk=>HandleChunk(chunk));
bb.LinkTo(ab, new DataflowLinkOptions(){PropogateCompletion = true});
for(int i = 0; i < 23; ++i)
{
bb.Post(i);
}
bb.Complete();
ab.Completion.Wait();
You could create an extension method on Queue<T>
:
public static class QueueExtensions
{
public static IEnumerable<T> DequeueChunk<T>(this Queue<T> queue, int chunkSize)
{
for (int i = 0; i < chunkSize && queue.Count > 0; i++)
{
yield return queue.Dequeue();
}
}
}
Usage:
var q = new Queue<char>();
q.DequeueChunk(10) // first 10 items
q.DequeueChunk(10) // next 10 items
Example: https://dotnetfiddle.net/OTcIZX