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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:50:55+00:00 2026-05-27T06:50:55+00:00

So I think this might be a fundamental flaw in my approach to threading

  • 0

So I think this might be a fundamental flaw in my approach to threading and incrementing a global counter, but here is my problem. I have a collection of file names from a DB that I iterate through, and for each file name I search it within a top level folder. Each iteration I thread the search and increment a counter when it completes so I can determine when it finishes. The problem is the counter never ever ever gets as high as the total file count, it comes very close sometimes, but never reaches what I would expect.

public class FindRogeRecords
{
    private delegate void FindFileCaller(string folder, int uploadedID, string filename);
    private Dictionary<int, string> _files;
    private List<int> _uploadedIDs;
    private int _filesProcessedCounter;
    private bool _completed;

    public void Run()
    {
        _files = GetFilesFromDB(); //returns a dictionary of id's and filenames
        FindRogueRecords();
    }

    private void FindRogueRecords()
    {
            _uploadedIDs = new List<int>();

        foreach (KeyValuePair<int, string> pair in _files)
        {
            var caller = new FindFileCaller(FindFile);
            var result = caller.BeginInvoke(txtSource.Text, pair.Key, pair.Value, new AsyncCallback(FindFile_Completed), null);
        }
    }

    private void FindFile(string documentsFolder, int uploadedID, string filename)
    {
        var docFolders = AppSettings.DocumentFolders;

        foreach (string folder in docFolders)
        {
            string path = Path.Combine(documentsFolder, folder);
            var directory = new DirectoryInfo(path);
            var files = directory.GetFiles(filename, SearchOption.AllDirectories);

            if (files != null && files.Length > 0) return;
        }

        lock (_uploadedIDs) _uploadedIDs.Add(uploadedID);
    }

    private void FindFile_Completed(System.IAsyncResult ar)
    {
        var result = (AsyncResult)ar;
        var caller = (FindFileCaller)result.AsyncDelegate;

        _filesProcessedCounter++;
        _completed = (_files.Count == _filesProcessedCounter); //this never evaluates to true
    }
}
  • 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-27T06:50:55+00:00Added an answer on May 27, 2026 at 6:50 am

    You are accessing _filesProcessedCounter variable from multiple threads without any synchronization (even simple lock()) so this caused Race Condition in your code.

    To increment an integer variable you can use Interlocked.Increment() which is thread-safe but considering that following line of code requires synchronization as well:

    _completed = (_files.Count == _filesProcessedCounter); 
    

    I would suggest using lock object to cover both lines and keep code much clear:

    // Add this field to a class fields list
    private static readonly object counterLock = new object();
    
    // wrap access to shared variables by lock as shown below
    lock (counterLock)
    {
      _filesProcessedCounter++;
      _completed = (_files.Count == _filesProcessedCounter);   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Soo, I think this might be early, but I have a need for usb
I think this might be a passing the argument problem, but I'm still new
I think this might be a problem with the theme I'm using ( Nitobe
I mostly develop using C#, but I think this question might be suitable for
I think my code might be written incorrectly and this is causing my problem.
I think this could be a very easy question for you. But I have
I think this might be a pretty simple question, but I haven't been able
I think this might be an easy question, but I'm just a bit stuck.
I think this might be an easy question for the professionals out there, but
I think this might be an IIS7 permissions thing, but I'm tagging it with

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.