Analogue of Queue.Peek() for BlockingCollection when listening to consuming IEnumerable

后端 未结 4 1709
梦谈多话
梦谈多话 2021-01-04 13:06

I\'m using Pipelines pattern implementation to decouple messages consumer from a producer to avoid slow-consumer issue.

In case of any exception on a message processi

4条回答
  •  别那么骄傲
    2021-01-04 13:33

    BlockingCollection is a wrapper around IProducerConsumerCollection, which is more generic than e.g. ConcurrentQueue and gives the implementer the freedom of not having to implement a (Try)Peek method.

    However, you can always call TryPeek on the underlying queue directly:

    ConcurrentQueue useOnlyForPeeking = new ConcurrentQueue();
    BlockingCollection blockingCollection = new BlockingCollection(useOnlyForPeeking);
    ...
    useOnlyForPeeking.TryPeek(...)
    

    Note however that you must not modify your queue via useOnlyForPeeking, otherwise blockingCollection will get confused and may throw InvalidOperationExceptions at you, but I'd be surprised if calling the non-modifying TryPeek on this concurrent data structure would be an issue.

提交回复
热议问题