December 2019
Intermediate to advanced
346 pages
9h 8m
English
The following code creates a new BlockingCollection that creates up to 10 items after which it goes to the blocked state before items are consumed by consumer threads:
BlockingCollection<int> blockingCollection = new BlockingCollection<int>(10);
Items can be added to the collection as follows:
blockingCollection.Add(1);blockingCollection.TryAdd(3, TimeSpan.FromSeconds(1))
Items can be removed from the collection as follows:
int item = blockingCollection.Take();blockingCollection.TryTake(out item, TimeSpan.FromSeconds(1))
The producer thread calls the CompleteAdding() method when there are no more items to add. This method, in turn, sets the IsAddingComplete property of the collection to true.
The consumer thread ...
Read now
Unlock full access