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

  • Home
  • SEARCH
  • 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 6056031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:19:51+00:00 2026-05-23T08:19:51+00:00

I am trying to update entries in a ConcurrentDictionary something like this: class Class1

  • 0

I am trying to update entries in a ConcurrentDictionary something like this:

class Class1
{
    public int Counter { get; set; }
}

class Test
{
    private ConcurrentDictionary<int, Class1> dict =
        new ConcurrentDictionary<int, Class1>();

    public void TestIt()
    {
        foreach (var foo in dict)
        {
            foo.Value.Counter = foo.Value.Counter + 1; // Simplified example
        }
    }
}

Essentially I need to iterate over the dictionary and update a field on each Value. I understand from the documentation that I need to avoid using the Value property. Instead I think I need to use TryUpdate except that I don’t want to replace my whole object. Instead, I want to update a field on the object.

After reading this blog entry on the PFX team blog: Perhaps I need to use AddOrUpdate and simply do nothing in the add delegate.

Does anyone have any insight as to how to do this?


I have tens of thousands of objects in the dictionary which I need to update every thirty seconds or so. Creating new ones in order to update the property is probably not feasible. I would need to clone the existing object, update it and replace the one in the dictionary. I’d also need to lock it for the duration of the clone/add cycle. Yuck.

What I’d like to do is iterate over the objects and update the Counter property directly if possible.

My latest research has led me to to Parallel.ForEach which sounds great but it is not supposed to be used for actions that update state.

I also saw mention of Interlocked.Increment which sounds great but I still need to figure out how to use it on each element in my dictionary in a thread safe way.

  • 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-23T08:19:52+00:00Added an answer on May 23, 2026 at 8:19 am

    First, to solve your locking problem:

    class Class1
    {
        // this must be a variable so that we can pass it by ref into Interlocked.Increment.
        private int counter;
    
        public int Counter
        {
            get{return counter; }
        }
    
        public void Increment()
        {
            // this is about as thread safe as you can get.
            // From MSDN: Increments a specified variable and stores the result, as an atomic operation.
            Interlocked.Increment(ref counter);
    
            // you can return the result of Increment if you want the new value,
            //but DO NOT set the counter to the result :[i.e. counter = Interlocked.Increment(ref counter);] This will break the atomicity.
        }
    }
    

    Iterating the just values should be faster than iterating the key value pair. [Though I think iterating a list of keys and doing the look-ups will be faster still on the ConcurrentDictionary in most situations.]

    class Test
    {
        private ConcurrentDictionary<int, Class1> dictionary = new ConcurrentDictionary<int, Class1>();
    
        public void TestIt()
        {
            foreach (var foo in dictionary.Values)
            {
                foo.Increment();
            }
        }
    
        public void TestItParallel()
        {
            Parallel.ForEach(dictionary.Values,x=>x.Increment() );
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to update the comments column in one table (Entries), when a new
I am trying update text views while this loop is processing. Here is my
When trying to update a subversion working copy from Netbeans, I get the following
I`m trying to update time in datetime field as UPDATE table_name SET col_name=to_DATE('04/02/2012 00:12:00','MM/DD/YYYY
I'm trying to create a sitemap that will automatically update. I've done something similiar
I am trying update my version of sqlite3 on mac os x 10.5.7 I
I trying to update a model on a callback but the validation is causing
Im trying to update the contents of an element after running some php code.
I'm trying to update a hashtable in a loop but getting an error: System.InvalidOperationException:
I am trying to update a column in my table which was last inserted.

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.