GetConsumingEnumerable returns an enumerable for a BlockingCollection<T> work queue. A foreach loop removes items as it processes them, waits when the queue is empty, and ends after CompleteAdding is called and queued items are exhausted. section 8.2.2
Use it to coordinate producers and consumers without manually implementing queue-waiting logic:
- Start one or more consumer threads, each iterating over the same consuming enumerable. section 13.2.2
- Add work from producer code; each item is processed once, although completion output can appear out of order with multiple consumers. section 13.2.2
- Call CompleteAdding when production ends so waiting consumers can exit after processing remaining items. section 8.2.2
- Combine the queue with bounded capacity when producers must block rather than grow the collection without limit. section 13.2.2
Background work queue
A background mail processor can run foreach (var current in _queue.GetConsumingEnumerable()), send each queued message, catch and log per-item failures, and stop naturally after _queue.CompleteAdding().
