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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:02:39+00:00 2026-06-03T11:02:39+00:00

I am currently writing a C# application. I am new to using a ConcurrentDictionary

  • 0

I am currently writing a C# application. I am new to using a ConcurrentDictionary so have some questions around its thread safety. Firstly, this is my dictionary:

    /// <summary>
    /// A dictionary of all the tasks scheduled
    /// </summary>
    private ConcurrentDictionary<string, ITask> tasks;

I instantiate this in my class and use it to track all of my objects that implement ITask. I want ensure my setup will work correctly in a multi threaded environment.

If multiple threads want to get the count of the number of items in the ConcurrentDictionary, do I need to lock it?

If I want to get a particular key from the dictionary, get the object of that key and call a method on it, do I need to lock it? eg:

    /// <summary>
    /// Runs a specific task.
    /// </summary>
    /// <param name="name">Task name.</param>
    public void Run(string name)
    {
        lock (this.syncObject)
        {
            var task = this.tasks[name] as ITask;
            if (task != null)
            {
                task.Execute();
            }
        }
    }

Keeping mind multiple threads could call the Run method looking to call the Execute method of ITask. My aim is to have everything thread safe and as performant as possible.

  • 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-03T11:02:41+00:00Added an answer on June 3, 2026 at 11:02 am

    The methods and properties of the ConcurrentDictionary themself are completely thread safe:

    http://msdn.microsoft.com/en-us/library/dd287191.aspx

    Represents a thread-safe collection of key-value pairs that can be
    accessed by multiple threads concurrently.

    This includes the Count property:

    Count has snapshot semantics and represents the number of items in the
    ConcurrentDictionary at the moment when Count was
    accessed.

    However this does not mean that the objects held inside the dictionary are themselves thread safe. That is, there is nothing stopping two threads from accessing the same Task object and trying to run the Execute method on it. If you’d like serialised (locked) access to each individual task for the Execute method, then I suggest having a locking object inside Task and locking when running Execute:

    public class Task
    {
        private object _locker = new object();
    
        public void Execute()
        {
            lock (_locker) {
               // code here
            }
        }
    }
    

    This ensures that at least every individual task doesn’t have multiple threads running Execute. I’m guessing this is what you’re after from the context of the question and the names of the classes and methods.

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

Sidebar

Related Questions

The application I'm currently writing is using MVVM with the ViewModel-first pattern. I have
Does anyone have experience writing a Facebook application using GWT with GAE? I'm new
I am currently writing an application where the user has, at some point, to
I'm currently writing an application using Vala that requires me to send data over
I'm currently writing a web application that have about 6-12 pages. On each one
I'm currently writing some intranet web application where people could submit to admins requests
I'm currently writing a geographical web application. Obviously it needs access to some geographical
I am currently writing a wizard-style application using Qt4. I am not using the
I'm currently writing a application that needs to capture some images. I start the
I'm currently working on a new web application and it's being developed using Oracle

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.