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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:10:58+00:00 2026-06-09T14:10:58+00:00

Even if I use lock with static locker object to lock my code from

  • 0

Even if I use lock with static locker object to lock my code from accessing the same item at the same time, how can I guarantee that this item is not removed by HttpRuntime from another thread during my lock ? I don’t see any SyncRoot property in HttpRuntime or HttpRuntime.Cache.

If I specify expiration callback and lock my static locker object inside that callback, would it be proper to do that ?

Is it bad to lock HttpRuntime.Cache expiration thread inside callback ?

  • 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-09T14:11:00+00:00Added an answer on June 9, 2026 at 2:11 pm

    Looking for a “SyncRoot” property suggests that you use the SyncRoot property when using locking in combination with a collection. There’s a reason why, although it is supported by later collections for backwards-compatibility, it tends to be hidden through implementing the interface explicitly. Really, the SyncRoot idea wasn’t a very good one.

    In this case, you’re talking about a thread-safe collection, so there’s even less need. System.Web.Caching.Cache does its own locking (or other mechanisms, it’s specified to be threadsafe rather than to be threadsafe through a particular approach), so adding, accessing and removing an item from the collection across several threads won’t corrupt it.

    The only remaining risk, is if either the object itself isn’t threadsafe, or if you access it from the collection several times in the same thread.

    The second is easily avoided by not doing so. If you do:

    (HttpRuntime.Cache.Get(someKey) as SomeType).SomeMethod();
    (HttpRuntime.Cache.Get(someKey) as SomeType).SomeOtherMethod();
    

    This only makes sense if you want to possibly be dealing with a different object in the second call than the first. Otherwise, it’s easily resolved:

    SomeType obj = HttpRuntime.Cache.Get(someKey) as SomeType;
    obj.SomeMethod();
    obj.SomeOtherMethod();
    

    (You also get a slight performance improvement by not calling the Get method repeatedly.

    If you need to worry about different threads calling SomeMethod() and SomeOtherMethod() at the same time you need to either ensure that those methods are threadsafe, or lock on an object that relates to the object in question. Most of the time the most obvious choice for an object that relates to an object, is that object itself. Hence:

    SomeType obj = HttpRuntime.Cache.Get(someKey) as SomeType;
    lock(obj)
    {
      obj.SomeMethod();
      obj.SomeOtherMethod();
    }
    

    (Note that even if SomeMethod() and SomeOtherMethod() were threadsafe, we might still need to do this if the combination of them wasn’t thread-safe. E.g. if the first reported on the objects state and we decided on the basis of that whether or not to perform the second, then we’d normally need to lock around that to prevent the state changing between the first and the second method call).

    Of course, all other operations on the object would need to be locked in the same manner. It gets more complicated if we’ve multiple objects we need to synchronise as a unit. Then we need a more complicated rule about what object we lock on that simply locking on the object in question; because there’s no single “object in question”.

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

Sidebar

Related Questions

It seems that we can show layers and even use a different zPosition for
What happens when we use the lock object? I am aware that it the
I am trying to use SpinLock, but even this most basic code in a
In my MVC 3 C# application I have some static object that I want
How can I configure the maximum memory that a query (select query) can use
How can I lock the version of a Maven plugin I want to use?
Is it possible (or even advisable) to use a PriorityExecutorBasedEventDrivenDispatcher with a TypedActor, in
My aplication needs to use device language even when it is running in background.
It looks like you cannot use a helper in a controller even if both
Is this the correct (or even a valid way) to use emums in Objective-C?

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.