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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:36:02+00:00 2026-06-12T03:36:02+00:00

I have an Object, a Product , which is stored in a local cache

  • 0

I have an Object, a Product, which is stored in a local cache in my app. This product cache is loaded from the database using some ORM code, which I can’t easily change. For simplicity, the Product is:

Public Class Product
    Public Property ID as Integer
    Public Property Name as String
    Public Property Price as Decimal
    Public Property Rank as Integer
End Class

Products are stored in a local cache:

   Public Class ProductList
       Public Shared Function GetCache as List(Of Product)
           Static _List as List(Of Product)
           If _List is Nothing then
               'Code to load Data and populate List from DB
           End If
           Return _List
       End Function
    End Class

This is heavily simplified, but you get the idea.

The Rank property is an unbound, calculated field based on all sales in the past 90 days, so that common products appear higher in a search. I want to generate the rank on a background thread – as it can be time consuming, and refresh it from time to time. Something like this:

Public Sub UpdateRank
    Dim dt as Datatable
    dt = GetDataTable("exec usp_GetProductRank") 'Returns [ID, Rank]
    For each row in dt.rows
        _prod = getCache.getbyID(row.item(0))
        If _prod isnot Nothing then
            _prod.rank = row.item(1)
        End If
    Next
End Sub

The cache may be used while the rank is being updated, resulting in dirty reads, but I am happy to live with that. What I do want to do is to perform this in a thread safe manner. That is, update the collection without worrying about if things are added and removed, etc.

Can I use a System.ComponentModel.BackgroundWorker to perform this update? I use lots of threading in the main UI for things like this, but this is a non-UI class in a DLL. I was thinking of pulling the cache into a Dictionary(of Integer, Product) first?

What is the best way to handle this?

  • 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-12T03:36:04+00:00Added an answer on June 12, 2026 at 3:36 am

    As you using shared write access to objects you will need to use locks or semaphores to avoid collisions. When doing this it will tell other threads to wait until the object is unlocked. This takes no cpu-resources. When the object is unlocked the next thread can access it on a first-come-first-served basis (in theory).

    F.ex:

    VB.Net:

    Class ThreadSafe
    
        Private Shared _locker As Object = New Object
    
        Private Shared Sub Go()
            SyncLock _locker
                If (_val2 <> 0) Then
                    Console.WriteLine((_val1 / _val2))
                End If
                _val2 = 0
            End SyncLock
        End Sub
    End Class
    

    C#:

    class ThreadSafe
    {
      static readonly object _locker = new object();
      static int _val1, _val2;
    
      static void Go()
      {
        lock (_locker)
        {
          if (_val2 != 0) Console.WriteLine (_val1 / _val2);
          _val2 = 0;
        }
      }
    }
    

    Example comes from a free e-book which is an excellent reference on multi-threading:
    http://www.albahari.com/threading/part2.aspx#_Locking

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

Sidebar

Related Questions

I have an application in which some of configurations are stored in database and
I have a productVariant object which has child product object. I want to show
I have an object OrderDetail. This has an ID, List of product objects and
I have created in-app purchase for non-consumable object application in iPhone using with store
I have a object (product), with a property of type 'array' e.g. product.tags =
I have a basic Customer/Order/OrderItem/Product object graph. Customer has Many Orders, Order has Many
In C# if I have the following object: IEnumerable<Product> products; and if I want
I have object A which contains multiple instances of object B, which in turn
I think I read somewhere that some modules only have object oriented interfaces (
I have different objects which is used to store the data from DB in

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.