Questions tagged [blockingcollection]

A .Net class that provides blocking and bounding capabilities for thread-safe collections.

A .Net class that provides blocking and bounding capabilities for thread-safe collections.

References

233 questions
106
votes
4 answers

Is there anything like asynchronous BlockingCollection?

I would like to await on the result of BlockingCollection.Take() asynchronously, so I do not block the thread. Looking for anything like this: var item = await blockingCollection.TakeAsync(); I know I could do this: var item = await Task.Run(()…
avo
  • 10,101
  • 13
  • 53
  • 81
46
votes
4 answers

What is the Difference between ArrayBlockingQueue and LinkedBlockingQueue

What scenarios is it better to use an ArrayBlockingQueue and when is it better to use a LinkedBlockingQueue? If LinkedBlockingQueue default capacity is equal to MAX Integer, is it really helpful to use it as BlockingQueue with default capacity?
Java_Jack
  • 577
  • 1
  • 5
  • 8
30
votes
5 answers

Using BlockingCollection: OperationCanceledException, is there a better way?

I'm making use of the (frankly great) BlockingCollection type for a heavily multithreaded, high-performance app. There's a lot of throughput through the collection and on the micro-level it's highly performant. However, for each 'batch' it will…
Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
23
votes
1 answer

Parallel.ForEach loop with BlockingCollection.GetConsumableEnumerable

Why Parallel.ForEach loop exits with OperationCancelledException, while using GetConsumableEnumerable? //outside the function static BlockingCollection _collection = new BlockingCollection(); var t =…
18
votes
4 answers

Why does iterating over GetConsumingEnumerable() not fully empty the underlying blocking collection

I have a quantifiable & repeatable problem using the Task Parallel Library, BlockingCollection, ConcurrentQueue & GetConsumingEnumerable while trying to create a simple pipeline. In a nutshell, adding entries to a default BlockingCollection
Eoin Campbell
  • 43,500
  • 17
  • 101
  • 157
16
votes
3 answers

How to consume a BlockingCollection in batches

I've come up with some code to consume all wating items from a queue. Rather than processing the items 1 by 1, it makes sense to process all waiting items as a set. I've declared my queue like this. private BlockingCollection items = new…
Jodrell
  • 34,946
  • 5
  • 87
  • 124
15
votes
3 answers

How to create no-duplicates ConcurrentQueue?

I need a concurrent collection that doesn't allow duplicates (to use in BlockingCollection as Producer/Consumer). I don't need strict order of elements. From another hand i want to minimize the maximum time of element "live" in collection. I.e.…
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
14
votes
4 answers

Element order in BlockingCollection<>

I have a Download Queue implemented with BlockingCollection<>. Now I want to prioritize some Download once in a while. I thought it might be great to move some elements 'up' the Collection, like in a list, but there is no method like…
13
votes
1 answer

Calling Dispose on an BlockingCollection

I've reused the example producer consumer queue from the C# in a Nutshell book of Albahari (http://www.albahari.com/threading/part5.aspx#_BlockingCollectionT) and a colleague remarked: "Why isn't the Dispose called on the BlockingCollection in the…
Sander
  • 616
  • 1
  • 6
  • 17
13
votes
1 answer

Is the BlockingCollection.TakeFromAny method suitable for building a blocking priority queue?

I need to build a blocking priority queue and my hunch is that TakeFromAny may be the secret ingredient, however the documentation on that method is sparse. What is its purpose / appropriate use? My requirement is that multiple threads will add to…
Ralph Shillington
  • 20,718
  • 23
  • 91
  • 154
11
votes
3 answers

Wait until a BlockingCollection queue is cleared by a background thread, with a timeout if it takes too long?

In C#, I'm wondering if it's possible to wait until a BlockingCollection is cleared by a background thread, with a timeout if it takes too long. The temporary code that I have at the moment strikes me as somewhat inelegant (since when is it good…
Contango
  • 76,540
  • 58
  • 260
  • 305
10
votes
4 answers

How to access the underlying default concurrent queue of a blocking collection?

I have multiple producers and a single consumer. However if there is something in the queue that is not yet consumed a producer should not queue it again. (unique no duplicates blocking collection that uses the default concurrent queue) if…
9
votes
1 answer

How to do async operations in a TPL Dataflow for best performance?

I wrote the following method to batch process a huge CSV file. The idea is to read a chunk of lines from the file into memory, then partition these chunk of lines into batches of fixed size. Once we get the partitions, send these partitions to a…
user330612
  • 2,189
  • 7
  • 33
  • 64
8
votes
2 answers

How to cancel GetConsumingEnumerable() on BlockingCollection

In the following code I'm using the CancellationToken to wake up the GetConsumingEnumerable() when the producer is not producing and I want to break out of the foreach and exit the Task. But I dont see IsCancellationRequested being logged and my…
Spud
  • 243
  • 1
  • 2
  • 12
8
votes
1 answer

How do I use a BlockingCollection in the Producer/Consumer pattern when the producers are also the consumers - How do I end?

I have a recursive problem where the consumers do some work at each level of a tree, then need to recurse down the tree and perform that same work at the next level. I want to use ConcurrentBag/BlockingCollection etc to run this in parallel. In this…
Jason Coyne
  • 6,509
  • 8
  • 40
  • 70
1
2 3
15 16