CompleteAdding marks a BlockingCollection<T> work queue as closed to new items. Consumers continue processing queued items, then exit when the queue is empty. section 8.2.2 section 13.2.2
Use the operation as the producer-consumer handoff:
- Add work items while production continues. section 8.2.2
- Call CompleteAdding after the producer has submitted the final item. section 8.2.2
- Consume with GetConsumingEnumerable; foreach blocks when empty and exits after all remaining items are processed. section 8.2.2 section 13.2.2
- Do not call Add or TryAdd after completion; either operation throws InvalidOperationException. section 13.2.2
Closing a background mail queue
A MailMerger enqueues prepared messages, while a dedicated background thread iterates over GetConsumingEnumerable and sends them. Stop calls _queue.CompleteAdding(), allowing the worker to finish queued mail before exiting.
