Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8770523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:34:21+00:00 2026-06-13T17:34:21+00:00

I have a BlockingCollection(ConcurrentBag, 50000) where I am trying to use a very small

  • 0

I have a BlockingCollection(ConcurrentBag, 50000) where I am trying to use a very small Bounded Capacity of 50,000 for the producer threads in order to maximize the number of records I can process in my consumer thread’s ConcurrentDictionary. The producer is much, much faster than the consumer and will consume most of the memory otherwise.

Unfortunately, I immediately noticed that the total number of records in my ConcurrentDictionary is now substantially lower than it should be after adding the bounded capacity of 50,000 when my test data executes. I read that the BlockingCollection’s .add method should block indefinitely until there is space in the collection for the add to execute. However, this does not appear to be the case.

Questions:

  1. Will a BlockingCollection’s .add method eventually time out or silently fail if too many add’s are called before capacity in the BlockingCollection frees up?

  2. If the answer to #1 is yes, how many adds can I attempt after the Bounding Capacity has been exceeded without losing data?

  3. If many BlockingCollection .add() methods are called which are waiting / blocking for capacity and the CompleteAdding() method is called, will those waiting / blocking adds continue to wait and then eventually add or do they silently fail?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:34:22+00:00Added an answer on June 13, 2026 at 5:34 pm

    Make sure that, if you are using a BlockingCollection together with a ConcurrentDictionary that you do not have a BlockingCollection.TryAdd(myobject) method hidden in your code somewhere and are mistaking it for the ConcurrentDictionary.TryAdd() method. The BlockingCollection.TryAdd(myobject) will return false and discard the add request producing a “silent fail”, if the BlockingCollection’s Bounding Capacity has been exceeded.

    1. The BlockingCollection’s .Add() method does not appear to “silenty fail” or lose add’s after exceeding the Bounding Capacity by a large amount. The add() method will eventually cause the process to run out of memory, if too many .add()’s are waiting to be added to a BlockingCollection that is over capacity. (This would have to be a very extreme case of flow-control issues)
    2. See #1.
    3. My own tests seem to indicate that once the CompleteAdding() method is called, all subsequent adds fail as described in the MSDN docs.

    A Final Note Regarding Performance

    It appears that (in my own case anyways) using a Bounding Capacity and .Add() on a BlockingCollection is very slow compared to using no Bounding Capacity and .TryAdd() in the same process.

    I achieved much better performance results by implementing my own Bounding Capacity strategy. There are many ways to do this. Three choices include Thread.Sleep(), Thread.Spinwait(), or Monitor.Wait() used together with Monitor.PulseAll(). When one of these strategies are used, it is possible to also use BlockingCollection.TryAdd() instead of BlockingCollection.Add() and have NO Bounding Capacity without losing any data or running out of memory. This method also seems to yield better performance.

    You can choose from the three examples based on which scenario works best for the speed differences in your Producer and Consumer threads.

    Thread.Wait() Example:

    //Check to see if the BlockingCollection's bounded capacity has been exceeded.
    while (Tokens.Count > 50000)
    {   //If the bounded capacity has been exceeded
        //place the thread in wait mode 
        Thread.Sleep(SleepTime);
    }
    

    Thread.SpinWait() Example:

    //Check to see if the BlockingCollection's bounded capacity has been exceeded.
    while (Tokens.Count > 50000)
    {   //If the capacity has been exceeded
        //place the thread in wait mode 
        Thread.SpinWait(SpinCount);
    }  
    

    Monitor.Wait() Example

    This example requires a hook in both the Producer and Consumer sides.

    Producer Code

    //Check to see BlockingCollection capacity has been exceeded.
    if (Tokens.Count > 50000)
    { 
        lock (syncLock)
        {   //Double check before waiting
            if (Tokens.Count > 50000)
            {
                Monitor.Wait(syncLock, 1000);
            }
        }
    }
    

    Consumer Code

    //Check to see BlockingCollection capacity is back a normal range.
    if (Tokens.Count <= 40000)
    { 
        lock (syncLock)
        {   //Double check before waiting
            if (Tokens.Count < 40000)
            {
                Monitor.PulseAll(syncLock);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have typical producer-consumer application. I have BlockingCollection to provide producing-consuming mechanism. What I
I'm using a BlockingCollection to implement a producer/consumer pattern. I have an asynchronous loop
I have the following async queue processing routing. var commandQueue = new BlockingCollection<MyCommand>(); commandQueue
Have a painfully simple blog Post creator, and I'm trying to check if the
Hello i have this code var queue = new BlockingCollection<int>(); queue.Add(0); var producers =
I have a scenario where I have multiple threads adding to a queue and
Let's say I have a business object that is very expensive to instantiate, and
In C# .NET 4.0 have a BlockingCollection that is taken from BlockingCollection Sample BC_AddTakeCompleteAdding
I have a thread adding items to a BlockingCollection . On another thread I
I have a bounded blocking queue in an application that was written several years

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.