Ejemplo n.º 1
0
		/// <summary>
		///     Attempts to obtain a lock on the specified object for up to
		///     the specified timeout.
		/// </summary>
		/// <param name="o"></param>
		/// <param name="timeout"></param>
		/// <returns></returns>
		public static TimedLock Lock(object o, TimeSpan timeout)
		{
			System.Threading.Thread.BeginCriticalRegion();
			var timedLock = new TimedLock(o);
			if (!Monitor.TryEnter(o, timeout))
			{
				// Failed to acquire lock.
#if DEBUG
				GC.SuppressFinalize(timedLock.leakDetector);
				throw new LockTimeoutException(o);
#else
            throw new LockTimeoutException();
#endif
			}
			return timedLock;
		}
Ejemplo n.º 2
0
        /// <summary>
        ///     Attempts to obtain a lock on the specified object for up to
        ///     the specified timeout.
        /// </summary>
        /// <param name="o"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public static TimedLock Lock(object o, TimeSpan timeout)
        {
            System.Threading.Thread.BeginCriticalRegion();
            var timedLock = new TimedLock(o);

            if (!Monitor.TryEnter(o, timeout))
            {
                // Failed to acquire lock.
#if DEBUG
                GC.SuppressFinalize(timedLock.leakDetector);
                throw new LockTimeoutException(o);
#else
                throw new LockTimeoutException();
#endif
            }
            return(timedLock);
        }