[dba-VB] c# lock()

jwcolby jwcolby at colbyconsulting.com
Thu Mar 10 07:12:44 CST 2011


I am using multi-threading and need to lock list objects so that two different threads can access 
them.  Do I even need to lock the list inside of the property as follows:

         public Dictionary<int, clsAZProcessFile> pLstFiles
         {
             get
             {
                 lock (lstFiles)
                 {
                     return lstFiles;
                 }

             }
         }

or only when I actually access the list?

If I create a property to return the list (without locking it in the property)

         public Dictionary<int, clsAZProcessFile> pLstFiles
         {
             get
             {
                 return lstFiles;
             }
         }

and I then get a pointer to the list using that property

	Dictionary<int, clsAZProcessFile> LstFiles;
	LstFiles = SomeObj.pLstFiles;

and I then lock the pointer while using it

	lock(LstFiles)
	{
		do stuff to list here...
	}

Does that in fact lock the list to other objects doing the same thing (getting the list through that 
property)?

IOW is the lock attribute affixed to the list at the list itself so that anything trying to use the 
list anywhere in the program can see that the list is locked?

-- 
John W. Colby
www.ColbyConsulting.com



More information about the dba-VB mailing list