//namespace Kit.Helpers //{ // /// Сервис для синхронизации потоков по ключу на основе словаря в памяти приложения // /// ключ синхронизации // public sealed class MemoryLockDictionaryService : ILockDictionaryService // { // private object _lockObjectLock; // private IDictionary _lockObjects; // public MemoryLockDictionaryService() // { // _lockObjectLock = new object(); // _lockObjects = new Dictionary(); // } // private string GetTypeMask(string type) => $"$t${(type ?? "no_type")}$t$"; // private string GetKeyMask(string key) => $"$k${(key ?? "no_key")}$k$"; // private string GetLockKey(string type, string key) => $"{GetTypeMask(type)}_{GetKeyMask(key)}"; // public int GetLockCount(string type) => _lockObjects.Count(x => x.Key.StartsWith(GetTypeMask(type))); // public int GetLockCount(string type, string key) => _lockObjects.Count(x => x.Key.Equals(GetLockKey(type, key))); // private object GetLockObject(string lockKey) // { // lock (_lockObjectLock) // { // if (_lockObjects.ContainsKey(lockKey) == false) // _lockObjects.Add(lockKey, new object()); // return _lockObjects[lockKey]; // } // } // private void RemoveLockObject(string lockKey) // { // // дожидаемся освобождения объекта // lock (this.GetLockObject(lockKey)) // { // } // // удаляем объект блокировки из очереди // lock (_lockObjectLock) // { // try // { // _lockObjects.Remove(lockKey); // } // catch { } // } // } // public void ExecuteOnLock(string type, string key, Action action) // { // string lockKey = GetLockKey(type, key); // lock (GetLockObject(lockKey)) // { // try // { // action(); // } // finally // { // RemoveLockObject(lockKey); // } // } // } // public TResult ExecuteOnLock(string type, string key, Func action) // { // string lockKey = GetLockKey(type, key); // lock (GetLockObject(lockKey)) // { // try // { // return action(); // } // finally // { // RemoveLockObject(lockKey); // } // } // } // } //}