Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demonstrates an InvalidOperationException when using BlockingCollection with ConcurrentBag as the underlying collection and calling TryTakeFromAny
using System;
using System.Collections.Concurrent;
using System.Threading;
namespace BlockingCollectionConcurrentBagException
{
class Program
{
static void Main(string[] args)
{
// We only hold a reference to the outer BlockingCollection, not the inner ConcurrentBag
var blockingCollection = new BlockingCollection<int>(new ConcurrentBag<int>());
// Produce elements into the collection.
var i = 0;
var producerFunc = new WaitCallback[1];
producerFunc[0] =
state =>
{
if (blockingCollection.Count == 0) blockingCollection.Add(i++);
else Thread.Yield();
ThreadPool.QueueUserWorkItem(producerFunc[0]);
};
ThreadPool.QueueUserWorkItem(producerFunc[0]);
// Concurrently pull elements off the collection.
var consumerFunc = new WaitCallback[1];
consumerFunc[0] =
ignored =>
{
var success = false;
do
{
try
{
// THIS IS WHERE THE EXCEPTION OCCURS
success = blockingCollection.TryTake(out _);
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
} while (success);
ThreadPool.QueueUserWorkItem(consumerFunc[0]);
};
// Start two competing consumers.
ThreadPool.QueueUserWorkItem(consumerFunc[0]);
ThreadPool.QueueUserWorkItem(consumerFunc[0]);
Thread.Sleep(-1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.