Peter's third link took me to an article by Jeffrey Richter and he has an
example of exactly what I need:

http://tinyurl.com/gobnp

Here is the sample:

class Transaction {

   // Private Object field used
   // purely for synchronization
   private Object objLock = new Object();

   // Private field holding the time of
   // the last transaction performed
   private DateTime timeOfLastTransaction;

   public void PerformTransaction() {
      lock (objLock) {
         // Perform the transaction...

         // Record time of the most recent transaction
         timeOfLastTransaction = DateTime.Now;
      }
   }

   // Public read-only property returning
   // the time of the last transaction
   public DateTime LastTransaction {
      get {
         lock (objLock) {
            // Return the time of the last transaction
            return timeOfLastTransaction;
         }
      }
   }
}

Thanks everybody for your input. Great learning experience.


-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] Behalf Of Sébastien
Lorion
Sent: Friday, August 11, 2006 11:44 AM
To: [email protected]
Subject: Re: [ADVANCED-DOTNET] Multithread-safe classes


Even the lock code snippet in VS 2005 suggests "this" as a default
value. How do they expect people to learn ?

Btw, locking on a string is not a good idea either because since
strings are immutable, they can be shared/interned, thus they may be
publicly accessible too.

Sébastien

On 8/11/06, Peter Ritchie
<[EMAIL PROTECTED]> wrote:
> Some references on what is good to lock and what is bad to lock upon:
> http://msdn2.microsoft.com/en-us/library/ms228970.aspx
> http://msdn2.microsoft.com/en-us/library/c5kehkcz.aspx
> http://haacked.com/archive/2005/04/12/2634.aspx
>
> Yes, it's still fairly common to see lock(this), even with sample code
> from Microsoft; it's just not suggested...
>
> ===================================
> This list is hosted by DevelopMentor(r)  http://www.develop.com
>
> View archives and manage your subscription(s) at
http://discuss.develop.com
>


--
Sébastien Lorion
Software Architect / Architecte organique
[EMAIL PROTECTED]

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to