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

  • Home
  • SEARCH
  • 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 7838631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:09:51+00:00 2026-06-02T15:09:51+00:00

The Application class in asp.net has Lock mechanism to support thread safety. As we

  • 0

The Application class in asp.net has Lock mechanism to support thread safety.

As we know – the Application can be accessed globally.

sample :

Application.Lock();
Application["MyCode"] = 21;
Application.UnLock();

ok.

but

Also the Cache is globally Accessible ( and doesnt have lock mechanism and also used to remove/ add items)

so why does Application has a lock mechanism and Cache Doesn’t ?

  • 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-02T15:09:53+00:00Added an answer on June 2, 2026 at 3:09 pm

    Application is a datastore remaining from old ASP technology. It has a single global lock. When you call Application.Lock() all accesses to Application object in all threads are blocked.

    On the other hand the newer Cache object which was introduced with ASP.NET allows you to use your own locking semantics. You can use .NET’s lock statement to ensure thread-safe access to Cache object while keeping your web application as parallel as possible. The lock statement is much safer since lock is guaranteed to be released when you exit a lock block. Application object doesn’t guarantee that. Cache also provides auto-expire mechanisms which is much more suited for, well, a cache. It can also expire keys based on dependency contracts and optional priorities which of course Application object lacks.

    I see no reason to use Application over Cache object.

    Example: Let’s say you have hundred items in the cache and you have a single item you want to store in the cache if it’s not already there. When you use Application, you do this:

    if(Application["someData"] == null) 
    {
        Application.Lock();
        if(Application["someData"] == null) 
        {
            Application["someData"] = getValue(); //a very long time consuming function
        }
        Application.Unlock();
    }
    

    In this scenario all accesses to Application object are blocked even if they are completely irrelevant. And in case getValue() causes an exception your Application is hung because the lock is not released. You need to wrap a try..finally around that to make sure it’s safe.

    On the other hand when using a Cache object, you do this:

    if(Cache["someData"] == null)
    {
        lock(myLockObject)  // or other shared lock for that single value
        {
            if(Cache["someData"] == null)
            {
                Cache["someData"] = getValue();
            }
        }
    }
    

    In this case only code blocks which require access to myLockObject would be waiting. Others which access to Cache would be running in parallel just fine. And in case getValue() throws an exception your lock is released without any issues letting other threads continue execution.

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

Sidebar

Related Questions

I am developing an asp.net mvc application, which has these enity classes: public class
I am working on a ASP.NET application that has a class that inherits a
Our main application has both a asp.net and winforms component. There is a class
I'm building a relatively simple class (ex: Person) for an ASP.NET application that has
i have asp.net mvc application where my model has a relation like Question can
In an ASP.NET MVC application where I have the following model.. public class EventViewModel
I am creating an ASP.NET MVC3 application using NHIBERNATE. I have a base class
I have the following class as part of an asp.net application. public sealed class
We're currently rewriting our organizations ASP.NET MVC application which has been written twice already.
I'm working on a web application. One of my co-workers has written some asp.net

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.