Пример #1
0
        public void Delete(string id)
        {
            AutoTimeoutObject ato = null;

            _lock.EnterWriteLock();
            try
            {
                _dict.Remove(id, out ato);
            }
            finally
            {
                _lock.ExitWriteLock();
            }

            (ato?.Object as IDisposable)?.Dispose();
        }
Пример #2
0
        public object Get(string id)
        {
            AutoTimeoutObject ato = null;

            _lock.EnterReadLock();
            try
            {
                bool ok = _dict.TryGetValue(id, out ato);
                if (ok && ato != null)
                {
                    Interlocked.Exchange(ref ato.CurrentTimeoutSec, 0);
                }
            }
            finally
            {
                _lock.ExitReadLock();
            }

            return(ato?.Object);
        }
Пример #3
0
        public string Add(object obj, int timeoutSec)
        {
            AutoTimeoutObject ato = new AutoTimeoutObject
            {
                TimeoutSec = timeoutSec,
                Object     = obj
            };
            string id = Guid.NewGuid().ToString();

            _lock.EnterWriteLock();
            try
            {
                _dict.Add(id, ato);
            }
            finally
            {
                _lock.ExitWriteLock();
            }

            return(id);
        }