Ejemplo n.º 1
0
 void Lock(Transaction transaction)
 {
     Monitor.Enter(this);
     if (OwningTransaction == null)
     {
         //Acquire the transaction lock if(transaction != null)
         OwningTransaction = transaction;
         Monitor.Exit(this);
         return;
     }
     else //Some transaction owns the lock
     {
         //We're done if it's the same one as the method parameter
         if (OwningTransaction == transaction)
         {
             Monitor.Exit(this);
             return;
         } //Otherwise, need to acquire the transaction lock
         else
         {
             ManualResetEvent manualEvent = new ManualResetEvent(false);
             KeyValuePair <Transaction, ManualResetEvent> pair = new KeyValuePair <Transaction, ManualResetEvent>(transaction, manualEvent);
             PendingTransactions.AddLast(pair);
             if (transaction != null)
             {
                 transaction.TransactionCompleted += (sender, e) =>
                 {
                     lock (this)
                     {
                         //Pair may have already been removed if unlocked
                         PendingTransactions.Remove(pair);
                     }
                     lock (manualEvent)
                     {
                         if (!manualEvent.SafeWaitHandle.IsClosed)
                         {
                             manualEvent.Set();
                         }
                     }
                 };
             }
             Monitor.Exit(this);
             //Block the transaction or the calling thread
             manualEvent.WaitOne();
             lock (manualEvent)
                 manualEvent.Close();
         }
     }
 }