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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:56:08+00:00 2026-05-16T05:56:08+00:00

I have an ASP.NET site that I am maintaining. Currently it has code that

  • 0

I have an ASP.NET site that I am maintaining. Currently it has code that on first access (and other times too) goes and does a bunch of data caching by writing it to files. Currently it has no locking so we cna get problems with multiple people accessing the site and multiple threads trying to write to the files at the same time. This is obviously bad so I am going to implement some locking.

My current modified code just puts a simple lock around a section of code so that later requests just wait until the first request is done.

My concern is that I haven’t used locks much so I just want to check if there is any situation in which that lock could not get released? For example I have no idea what happens if that first thread is killed (eg the web server decides its run too long and shuts it down). Does the lock get automatically freed at that point? Is there anything else I need to think about while doing this?

Edit to add:
Here is what I think are relevant bits of Code in case there is anything I am doing stupid…

I am using private lock objects accessed through a dictionary and don’t do anything much more than wrap some code in a SyncLock statement (equivalent of C# lock).

Public Shared Sub CheckAllVariables(ByVal SourceName As String, ByVal cn As HttpContext)
    ...
    Do While dr.Read
        projectID = dr.GetInt32(dr.GetOrdinal("ProjectID"))
        Dim cacheLockObject As Object = GetCacheLockObject(projectID)
        SyncLock (cacheLockObject)
            srcName = String.Format("PROJECT_{0}", projectID)
            If cacheCon.CheckNeeded(srcName) Then
                RunFullCache(projectID, cn, Nothing)
                CheckDerivedVariables(projectID, cn)
                CheckHierarchyVariables(projectID, cn)
                cn.Session(String.Format("DerivedChecked_{0}", projectID)) = True
                projectNames.Add(srcName)
                cacheCon.CheckNeeded(srcName) = False
            End If
        End SyncLock
    Loop
    ...
End Sub


Private Shared CacheLockObjects As New Dictionary(Of Integer, Object)
Public Shared Function GetCacheLockObject(ByVal projectid As Integer) As Object
    If Not CacheLockObjects.ContainsKey(projectid) Then
        CacheLockObjects(projectid) = New Object()
    End If
    Return CacheLockObjects(projectid)
End Function

Should I wrap access to the GetCacheLockObject function in a lock to prevent the possibility of two threads going in simultaneously and both finding the cache lock doesn’t exist and then both creating it and returning different ones? I’m a little unused to having to consider thread safeness (assumign I am using that term correctly).

  • 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-16T05:56:09+00:00Added an answer on May 16, 2026 at 5:56 am

    since lock is just Monitor calls in try { } finally { } it can’t get stuck. The application can get stuck if it gets into deadlock state. So you should be careful while locking on several objects.

    BTW, consider using database. It handles all those tasks and you will not worry about locks and writing to file. Plus a lot of other features.

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

Sidebar

Related Questions

I have an ASP.NET web site that has a entities version 4.0 DAL. I
I have Asp.Net MVC 3 site that has SSL on entire site. I do
I have an ASP.Net MVC site that has a subdomain for each customer e.g.
I have an ASP.NET 4.0 site that has links to another ASP.NET site (different
I have an old ASP.NET 1.1 site that I am maintaining. We are working
I have an ASP.NET Site that has a single Master Page. On one of
I have an ASP.NET site that has been running perfectly for a long time,
I have an ASP.NET site that is not currently using MVC but I'm trying
I have an ASP.NET site that I am trying to access div elements by
I have a page on my ASP.NET site that has a Repeater control to

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.