title: Dictionary Behaves Strangely During Databinding
body: <p>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.  </p>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>    OrderLabelAPI.MultiSyringeCacheEnabled = True
      Me.GridLabels.DataBind()
      OrderLabelAPI.MultiSyringeCacheEnabled = False
  </code></pre>
  
  <p>And the expensive call where the caching happens looks like this:</p>
  
  <pre><code>    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
  </code></pre>
  
  <p>And here is the MultiSyringeCacheEnabled property:</p>
  
  <pre><code>    Public Shared Property MultiSyringeCacheEnabled As Boolean
          Get
              Return m_MultiSyringeCacheEnabled
          End Get
          Set(value As Boolean)
              ClearMultiSyringeCache()
              m_MultiSyringeCacheEnabled = value
          End Set
      End Property
  </code></pre>
  
  <p>Very, very rarely (unreproducably rare...) I will get the following exception: The given key was not present in the dictionary.</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>I've actually gone ahead and yanked this code from the project, but I want to understand what I'm missing. Thanks!</p>