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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:33:02+00:00 2026-05-30T09:33:02+00:00

Let’s imagine I have this class : public class FileData { public Weigth {

  • 0

Let’s imagine I have this class :

public class FileData
{
   public Weigth { get; set; } // Not the file size, but a business weight
   public Name { get; set; } // Name of the file
   public FullPath { get; set; } // full path of the file
}

I want to explore a bunch of separate folders, from various network location. The idea is to be able, given a specific file name, to retrieve all candidate files, ordered by the weight of the file.

I want to split the exploration of each folder in a separate thread. in this case, my “store” should be concurrent access aware.

I’ve tried to use a ConcurrentDictionary<string, SortedSet<CdlFileInfo>> class to store the result of the exploration.

However, I’m a bit struggling with the correct way of populating the inner sortedset.

I’ve tried this :

class Program 
{
    private readonly static ConcurrentDictionary<string, SortedSet<CdlFileInfo>> g_Files = new ConcurrentDictionary<string,SortedSet<CdlFileInfo>>();
    public static void Main()
    {
        PopulateFileList();

        // Do something with the list of files
    }
    private static void PopulateFileList()
    {
        var sources = AnyMethodToGetFoldersList(); // IEnumarable<string>
        sources.AsParallel().ForAll(x =>
        {
            Console.WriteLine("Enumerating files in {0}", x.Folder);
            var allFiles = Directory.GetFiles(x.Folder, "*.*", SearchOption.AllDirectories);

            foreach (var file in allFiles)
            {
                var fd = new FileData {
                    Weigth = GetWeigth(file), // returns a int... the method is not important
                    Name = Path.GetFileName(file),
                    FullPath = file
                };

                            // Here is the key piece of my code

                g_Files.AddOrUpdate(
                    fileName,
                    new SortedSet<FileData>() { fd },
                    (filePath, source) =>
                    {
                        g_Files[fileName].Add(fd);
                        return g_Files[fileName];
                    }
                );
            }
        });
    }

    public class FileData
    {
       public Weigth { get; set; } // Not the file size, but a business weight
       public Name { get; set; } // Name of the file
       public FullPath { get; set; } // full path of the file
    }
    public class FileDataWeightComparer : IComparer<CdlFileInfo>
    {
        public int Compare(FileData x, FileData y)
        {
            return Comparer<int>.Default.Compare(x.Weigth,y.Weigth);
        }
    }
}

This code “seems” to work. Is it the correct way ? Does this code prevent threading issues when accessing an existing SortedSet ?

This code does not seems to works. I can see that some of the found values are lost. I suspect the last argument of AddOrUpdate method not to properly lock the inner SortedSet.

  • 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-30T09:33:03+00:00Added an answer on May 30, 2026 at 9:33 am

    Change this part of your code:

    (filePath, source) =>
    {
        g_Files[fileName].Add(fd);
        return g_Files[fileName];
    }
    

    To this:

    (filePath,source) =>
    {
        source.Add(fd);
        return source;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let's say i have this public class MyClass { public string myMessage { get;
Let's imagine I have a Java class of the type: public class MyClass {
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say that I have a set of relations that looks like this: relations
Let's say we have the following: abstract class A; class B : public A;
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I can call a method like this: core::get() . What is the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say I have this code: <p dataname=description> Hello this is a description. <a

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.