CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Proper locking for static Properties in threaded/web Environments

263 Views
Copy Code Show/Hide Line Numbers
private static object _SyncLock = new object();
 
public TableInfo<TEntity> TableInfo
{
  get 
  {
      if (_TableInfo == null)
      {
          lock (_SyncLock)
          {
              if (_TableInfo == null)
                _TableInfo = new TableInfo<TEntity>();
          }
      }
 
      return _TableInfo; 
  }
  set { _TableInfo = value; }
}
private static TableInfo<TEntity> _TableInfo = null;
by Rick Strahl
  August 23, 2009 @ 9:19pm
Tags:
Description:
Note the double check for null outside (to avoid locking every time) and again inside of the lock to ensure the value hasn't already been changed.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate