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

The Archive Base Latest Questions

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

I’m trying to make my application thread safe. I hold my hands up and

  • 0

I’m trying to make my application thread safe. I hold my hands up and admit I’m new to threading so not sure what way to proceed.

To give a simplified version, my application contains a list.

  • Most of the application accesses this list and doesn’t change it but
    may enumerate through it. All this happens on the UI thread.
  • Thread
    one will periodically look for items to be Added and Removed from the
    list.
  • Thread two will enumerate the list and update the items with
    extra information. This has to run at the same time as thread one as
    can take anything from seconds to hours.

The first question is does anyone have a recommend stragy for this.

Secondly I was trying to make seperate copies of the list that the main application will use, periodically getting a new copy when something is updated/added or removed, but this doesn’t seem to be working.

I have my list and a copy……

public class MDGlobalObjects
{
    public List<T> mainList= new List<T>();
    public List<T> copyList
    {
        get
        {
            return new List<T>(mainList);
        }
    }
}

If I get copyList, modify it, save mainlist, restart my application, load mainlist and look again at copylist then the changes are present. I presume I’ve done something wrong as copylist seems to still refer to mainlist.

I’m not sure if it makes a difference but everything is accessed through a static instance of the class.

public static MDGlobalObjects CacheObjects = new MDGlobalObjects();
  • 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-03T02:19:41+00:00Added an answer on June 3, 2026 at 2:19 am

    This is the gist using a ConcurrentDictionary:


    public class Element
    {
        public string Key { get; set; }
        public string Property { get; set; }
    
        public Element CreateCopy()
        {
            return new Element
            {
                Key = this.Key,
                Property = this.Property,
            };
        }
    }
    

    var d = new ConcurrentDictionary<string, Element>();
    
    // thread 1
    // prune
    foreach ( var kv in d )
    {
        if ( kv.Value.Property == "ToBeRemoved" )
        {
            Element dummy = null;
            d.TryRemove( kv.Key, out dummy );
        }
    }
    
    // thread 1
    // add
    Element toBeAdded = new Element();
    // set basic properties here
    d.TryAdd( toBeAdded.Key, toBeAdded );
    
    // thread 2
    // populate element
    Element unPopulated = null;
    if ( d.TryGetValue( "ToBePopulated", out unPopulated ) )
    {
        Element nowPopulated = unPopulated.CreateCopy();
    
        nowPopulated.Property = "Populated";
    
        // either
        d.TryUpdate( unPopulated.Key, nowPopulated, unPopulated );
    
        // or
        d.AddOrUpdate( unPopulated.Key, nowPopulated, ( key, value ) => nowPopulated );
    }
    
    // read threads
    // enumerate
    foreach ( Element element in d.Values )
    {
        // do something with each element
    }
    
    // read threads
    // try to get specific element
    Element specific = null;
    if ( d.TryGetValue( "SpecificKey", out specific ) )
    {
        // do something with specific element
    }
    

    In thread 2, if you can set properties so that the whole object is consistent after each atomic write, then you can skip making a copy and just populate the properties with the object in place in the collection.

    There are a few race conditions in this code, but they should be benign in that readers always have a consistent view of the collection.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT

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.