public override void TransferMoney(decimal amt, Account.Account otherAccount) { var orders = getLockOrder(this, otherAccount as holdwaitAccount);//Resolve DeadLock lock (orders[0].Lock) { Thread.Sleep(20); lock (orders[1].Lock) { this.Credit(amt); otherAccount.Debit(amt); Thread.Sleep(2); Console.WriteLine(amt + " Transfered from " + this.orderNumber + " To " + otherAccount.orderNumber); } } }
public override void TransferMoney(decimal amt, Account.Account otherAccount) { Mutex[] locks = { this.Lock, (otherAccount as MutexAccount).Lock }; if (WaitHandle.WaitAll(locks, 1000))//All Locks or one approach { try { this.Credit(amt); Thread.Sleep(20); otherAccount.Debit(amt); Console.WriteLine(amt + " Transfered from " + this.orderNumber + " To " + otherAccount.orderNumber); } catch (Exception ex) { Console.WriteLine(ex.Message); } }