public async Task <IDisposable> Lock(string key, TimeSpan?timeout)
        {
            timeout = timeout ?? TimeSpan.FromSeconds(10);

            UsingLock theLock;

            lock (key)
            {
                theLock = _lockDic.GetOrCreate(key, cacheEntry =>
                {
                    var newLock = new UsingLock();

                    cacheEntry.SlidingExpiration = TimeSpan.FromSeconds(30);
                    cacheEntry.RegisterPostEvictionCallback((_, _, reason, _) =>
                    {
                        if (reason == EvictionReason.Expired ||
                            reason == EvictionReason.TokenExpired ||
                            reason == EvictionReason.Removed)
                        {
                            newLock.DisposeLock();
                        }
                    });

                    return(newLock);
                });
            }

            await theLock.Wait(timeout.Value);

            return(theLock);
        }
Пример #2
0
 public MyQueue(IEnumerable <string> strings)
 {
     _List = new List <string>(strings);
     _Lock = new UsingLock <object>();
 }