using KLHZ.Trader.Core.Contracts.Messaging.Dtos.Interfaces; namespace KLHZ.Trader.Core.Exchange.Models.AssetsAccounting { public abstract class LockableExchangeObject : ILockableObject { private readonly SemaphoreSlim _sem = new SemaphoreSlim(1, 1); public Task Lock(TimeSpan duration) { var lockerTask = _sem.WaitAsync(0); _ = lockerTask.ContinueWith(async (t) => { if (t.Result) { await Task.Delay(duration); _sem.Release(); } }); return lockerTask; } public void Unlock() { try { _sem.Release(); } catch { } } } }