Dequeueing objects from a ConcurrentQueue in C#

后端 未结 3 853
野趣味
野趣味 2020-12-31 15:00

Hey, I\'m trying to implement a ConcurrentQueue in C# for an asynchronous server. Items are being queued as soon as a complete message is received. To dequeue messages, I\

3条回答
  •  悲哀的现实
    2020-12-31 15:24

    Instead of using ConcurrentQueue directly, have you tried wrapping it in a BlockingCollection? Then you can use TryTake(out T, TimeSpan) etc. I believe that's the expected use: that the concurrent collections themselves would usually be there just so you can select how the blocking collection will work.

    That doesn't have to be the only use for these collections of course, but particularly for ConcurrentQueue, the producer/consumer queue scenario is the most common one - at which point BlockingCollection is the way to make it easy to do the right thing.

提交回复
热议问题