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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:25:20+00:00 2026-06-05T15:25:20+00:00

I was trying to do a little data access optimization, and I ran into

  • 0

I was trying to do a little data access optimization, and I ran into a situation where a dictionary appeared to get out of sync in a way that should be impossible, unless I’m somehow getting into a multithreaded situation without knowing it.

One column of GridLabels binds to a property that does data access — which is a tad expensive. However, multiple rows end up making the same call, so I should be able to head any problems off at the pass by doing a little caching.

However, elsewhere in the app, this same code is called in ways where caching would not be appropriate, I needed a way to enable caching on demand. So my databinding code looks like this:

    OrderLabelAPI.MultiSyringeCacheEnabled = True
    Me.GridLabels.DataBind()
    OrderLabelAPI.MultiSyringeCacheEnabled = False

And the expensive call where the caching happens looks like this:

    Private Shared MultiSyringeCache As New Dictionary(Of Integer, Boolean)
    Private Shared m_MultiSyringeCacheEnabled As Boolean = False

    Public Shared Function IsMultiSyringe(orderLabelID As Integer) As Boolean
        If m_MultiSyringeCacheEnabled Then
            'Since this can get hit a lot, we cache the values into a dictionary. Obviously,
            'it goes away after each request. And the cache is disabled by default.
            If Not MultiSyringeCache.ContainsKey(orderLabelID) Then
                MultiSyringeCache.Add(orderLabelID, DoIsMultiSyringe(orderLabelID))
            End If

            Return MultiSyringeCache(orderLabelID)
        Else
            Return DoIsMultiSyringe(orderLabelID)
        End If
    End Function

And here is the MultiSyringeCacheEnabled property:

    Public Shared Property MultiSyringeCacheEnabled As Boolean
        Get
            Return m_MultiSyringeCacheEnabled
        End Get
        Set(value As Boolean)
            ClearMultiSyringeCache()
            m_MultiSyringeCacheEnabled = value
        End Set
    End Property

Very, very rarely (unreproducably rare…) I will get the following exception: The given key was not present in the dictionary.

If you look closely at the caching code, that’s impossible since the first thing it does is ensure that the key exists. If DoIsMultiSyringe tampered with the dictionary (either explicitly or by setting MultiSyringeCacheEnabled), that could also cause problems, and for awhile I assumed this had to be the culprit. But it isn’t. I’ve been over the code very carefully several times. I would post it here but it gets into a deeper object graph than would be appropriate.

So. My question is, does datagridview databinding actually get into some kind of zany multithreaded situation that is causing the dictionary to seize? Am I missing some aspect of shared members?

I’ve actually gone ahead and yanked this code from the project, but I want to understand what I’m missing. Thanks!

  • 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-05T15:25:21+00:00Added an answer on June 5, 2026 at 3:25 pm

    Since this is ASP.NET, you have an implicit multithreaded scenario. You are using a shared variable (see What is the use of a shared variable in VB.NET?), which is (as the keyword implies) “shared” across multiple threads (from different people visiting the site).

    You can very easily have a scenario where one visitor’s thread gets to here:

    'Since this can get hit a lot, we cache the values into a dictionary. Obviously,
    'it goes away after each request. And the cache is disabled by default.
    If Not MultiSyringeCache.ContainsKey(orderLabelID) Then
        MultiSyringeCache.Add(orderLabelID, DoIsMultiSyringe(orderLabelID))
    End If
    
    ' My thread is right here, when you visit the site
    
    Return MultiSyringeCache(orderLabelID)
    

    and then your thread comes in here and supercedes my thread:

    Set(value As Boolean)
        ClearMultiSyringeCache()
        m_MultiSyringeCacheEnabled = value
    End Set
    

    Then my thread is going to try to read a value from the dictionary after you’ve cleared it.

    That said, I am not sure what performance benefit you expect from a “cache” that you clear with every request. It looks like you should simply not make this variable shared- make it an instance variable- and any user request accessing it will have their own copy.

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

Sidebar

Related Questions

I ran into a little error trying to select off of my table. I
I am trying out a little reflection and have a question on how the
I'm going a little nuts trying to figure out how to use template inheritance
I'm trying to write a little Emacs Lisp script that reads a csv file
I've been working on an application that has no discernable data access layer, so
In trying to solve the problem of getting data from Drupal into XML, I
I get the data type mismatch in criteria expression error when trying insert a
i am trying escape some data before it goes into my database, but i
I'm trying to write a program that is just a little beyond my abilities.The
I'm trying to work with java data access, so this with what I came

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.