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 7027107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:12:16+00:00 2026-05-28T00:12:16+00:00

I am trying to convert an existing process in a way that it supports

  • 0

I am trying to convert an existing process in a way that it supports multi-threading and concurrency to make the solution more robust and reliable.

Take the example of an emergency alert system. When a worker clocks-in, a new Recipient object is created with their information and added to the Recipients collection. Conversely, when they clock-out, the object is removed. And in the background, when an alert occurs, the alert engine will iterate through the same list of Recipients (foreach), calling SendAlert(…) on each object.

Here are some of my requirements:

  • Adding a recipient should not block if an alert is in progress.
  • Removing a recipient should not block if an alert is in progress.
  • Adding or removing a recipient should not affect the list of
    recipients used by an in-progress alert.

I’ve been looking at the Task and Parallel classes as well as the BlockingCollection and ConcurrentQueue classes but am not clear what the best approach is.

Is it as simple as using a BlockingCollection? After reading a ton of documentation, I’m still not sure what happens if Add is called while I am enumerating the collection.

UPDATE

A collegue referred me to the following article which describes the ConcurrentBag class and how each operation behaves:

http://www.codethinked.com/net-40-and-system_collections_concurrent_concurrentbag

Based on the author’s explanation, it appears that this collection will (almost) serve my purposes. I can do the following:

  1. Create a new collection

    var recipients = new ConcurrentBag();

  2. When a worker clocks-in, create a new Recipient and add it to the collection:

    recipients.Add(new Recipient());

  3. When an alert occurs, the alert engine can iterate through the collection at that time because GetEnumerator uses a snapshot of the collection items.

    foreach (var recipient in recipients)
    recipient.SendAlert(…);

  4. When a worker clocks-out, remove the recipient from the collection:

    ???

The ConcurrentBag does not provide a way to remove a specific item. None of the concurrent classes do as far as I can tell. Am I missing something? Aside from this, ConcurrentBag does everything I need.

  • 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-05-28T00:12:17+00:00Added an answer on May 28, 2026 at 12:12 am

    ConcurrentBag<T> should definitely be the best performing class out of the bunch for you to use for such a case. Enumeration works exactly as your friend describes and so it should serve well for the scenario you have laid out. However, knowing you have to remove specific items from this set, the only type that’s going to work for you is ConcurrentDictionary<K, V>. All the other types only offer a TryTake method which, in the case of ConcurrentBag<T>, is indeterminate or, in the case of ConcurrentQueue<T> or ConcurrentStack<T> ordered only.

    For broadcasting you would just do:

    ConcurrentDictionary<string, Recipient> myConcurrentDictionary = ...;
    
    ...
    
    foreach(Recipient recipient in myConcurrentDictionary.Values)
    {
        ...
    }
    

    The enumerator is once again a snapshot of the dictionary in that instant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to convert an existing Android, SQLite database to a multi-platform, SQLite
I am trying to convert an existing HTML form and make it appear in
I am trying to convert a pre-existing site that had html and php intermingled
I'm trying to convert my existing site to use WCF to communicate with my
I'm trying to convert an existing SVN repository to GIT using git-svn clone but
I'm trying to to convert an existing PHP regular expression to apply to a
I am trying to migrate existing code that uses XmlSerializer to protobuf-net due to
I am trying to convert my existing PHP webpage to use UTF-8 encoding. To
I’m trying to convert an existing Windows CVS repository to Mercurial and getting the
i'm quite new to Dojo, but i am trying to convert an existing javascript

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.