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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:13:36+00:00 2026-05-24T14:13:36+00:00

In continuation for my latest ponders about locks in C# and .NET, Consider the

  • 0

In continuation for my latest ponders about locks in C# and .NET,

Consider the following scenario:

I have a class which contains a specific collection (for this example, i’ve used a Dictionary<string, int>) which is updated from a data source every few minutes using a specific method which it’s body you can see below:

    DataTable dataTable = dbClient.ExecuteDataSet(i_Query).GetFirstTable();

    lock (r_MappingLock)
    {
        i_MapObj.Clear();

        foreach (DataRow currRow in dataTable.Rows)
        {
            i_MapObj.Add(Convert.ToString(currRow[i_Column1]), Convert.ToInt32[i_Column2]));
        }
    }

r_MappingLock is an object dedicated to lock the critical section which refreshes the dictionary’s contents.

i_MapObj is the dictionary object

i_Column1 and i_Column2 are the datatable’s column names which contain the desired data for the mapping.

Now, I also have a class method which receives a string and returns the correct mapped int based on the mentioned dictionary.

I want this method to wait until the refresh method completes it’s execution, so at first glance one would consider the following implementation:

    lock (r_MappingLock)
    {
        int? retVal = null;

        if (i_MapObj.ContainsKey(i_Key))
        {
            retVal = i_MapObj[i_Key];
        }

        return retVal;
    }

This will prevent unexpected behaviour and return value while the dictionary is being updated, but another issue arises:
Since every thread which executes the above method tries to claim the lock, it means that if multiple threads try to execute this method at the same time, each will have to wait until the previous thread finished executing the method and try to claim the lock, and this is obviously an undesirable behaviour since the above method is only for reading purposes.

I was thinking of adding a boolean member to the class which will be set to true or false wether the dictionary is being updated or not and checking it within the “read only” method, but this arise other race-condition based issues…

Any ideas how to solve this gracefully?

Thanks again,

Mikey

  • 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-24T14:13:38+00:00Added an answer on May 24, 2026 at 2:13 pm

    Have a look at the built in ReaderWriterLock.

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

Sidebar

Related Questions

This post is an continuation of this post I have DlUser Class each object
This is a continuation of Why do we have to set __block variable to
Ok - this is in continuation from my earlier question about sending an email
This is a continuation of Confused as to which Prototype helper to use .
This is a continuation of the question here: JBoss - does app have to
my question is continuation of what i have asked see the link. Load Country/State/City
In continuation of this question I ask the following question. I've managed to read
This is a continuation of my last question: Internal Database - ASP.NET Auto Web
This question is in continuation of this post , I have tried installing Xerces-C
Continuation of thread: C# Invalidate troubles . I created the class but now I

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.