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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:29:20+00:00 2026-06-08T12:29:20+00:00

Are the following assumptions valid for this code? I put some background info under

  • 0

Are the following assumptions valid for this code? I put some background info under the code, but I don’t think it’s relevant.

Assumption 1: Since this is a single application, I’m making the assumption it will be handled by a single process. Thus, static variables are shared between threads, and declaring my collection of lock objects statically is valid.

Assumption 2: If I know the value is already in the dictionary, I don’t need to lock on read. I could use a ConcurrentDictionary, but I believe this one will be safe since I’m not enumerating (or deleting), and the value will exist and not change when I call UnlockOnValue().

Assumption 3: I can lock on the Keys collection, since that reference won’t change, even if the underlying data structure does.

private static Dictionary<String,Object> LockList = 
    new Dictionary<string,object>();

private void LockOnValue(String queryStringValue)
{
    lock(LockList.Keys)
    {
        if(!LockList.Keys.Contains(queryStringValue))
        {
            LockList.Add(screenName,new Object());
        }
        System.Threading.Monitor.Enter(LockList[queryStringValue]);
    }
}

private void UnlockOnValue(String queryStringValue)
{
    System.Threading.Monitor.Exit(LockList[queryStringValue]);
}

Then I would use this code like:

LockOnValue(Request.QueryString["foo"])
//Check cache expiry
//if expired
    //Load new values and cache them.
//else
    //Load cached values
UnlockOnValue(Request.QueryString["foo"])

Background: I’m creating an app in ASP.NET that downloads data based on a single user-defined variable in the query string. The number of values will be quite limited. I need to cache the results for each value for a specified period of time.

Approach: I decided to use local files to cache the data, which is not the best option, but I wanted to try it since this is non-critical and performance is not a big issue. I used 2 files per option, one with the cache expiry date, and one with the data.

Issue: I’m not sure what the best way to do locking is, and I’m not overly familiar with threading issues in .NET (one of the reasons I chose this approach). Based on what’s available, and what I read, I thought the above should work, but I’m not sure and wanted a second opinion.

  • 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-08T12:29:21+00:00Added an answer on June 8, 2026 at 12:29 pm

    Your current solution looks pretty good. The two things I would change:

    1: UnlockOnValue needs to go in a finally block. If an exception is thrown, it will never release its lock.

    2: LockOnValue is somewhat inefficient, since it does a dictionary lookup twice. This isn’t a big deal for a small dictionary, but for a larger one you will want to switch to TryGetValue.

    Also, your assumption 3 holds – at least for now. But the Dictionary contract makes no guarantee that the Keys property always returns the same object. And since it’s so easy to not rely on this, I’d recommend against it. Whenever I need an object to lock on, I just create an object for that sole purpose. Something like:

    private static Object _lock = new Object();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code works as expected for me under 64-bits, but fails under 32-bit
Following chunk html code works as expected: <iframe src=http://www.amazon.com/></iframe> But when trying embed inner
Following leads in this thread and others, I've set up a code block where
Consider this scenario with the following assumptions: The database is used for a non-critical
I have the following code (simplified to get to the point): - @assumptions[1].each do
Following on from this initial investigations on Silverlight architectures, I have some new requirements
Following code produces a nested array as a result for keys containing three items:
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Are the following assumptions accurate? 1) execute immediately (function(){ })(); 2) execute on document

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.