public bool TryAcquire(object syncRoot, int timeout, out ISyncScope syncScope) { if (syncRoot is null || _isDisposed) { syncScope = null; return(false); } var coordinator = _syncManager.GetCoordinator(syncRoot, true); if (_isDisposed) { syncScope = null; return(false); } if (!coordinator.TryAcquire(this, timeout) || !RegisterIfNotDisposed(coordinator)) { syncScope = null; return(false); } syncScope = new SyncScope(this, syncRoot); return(true); }
/// <summary> /// Attempts to acquire an <see cref="ISyncScope"/> within its own <see cref="ISyncContext"/> for /// the specified <paramref name="syncRoot"/> within the specified <paramref name="timeout"/>. /// See <see cref="ISyncScope"/> for more information on the blocking behavior of an acquired <see cref="ISyncScope"/>. /// </summary> /// <param name="syncRoot"> /// An object used to identify the resource for which the acquired <see cref="ISyncScope"/> /// will provide synchronization. /// </param> /// <param name="timeout">The time, in milliseconds, to wait for the <see cref="ISyncScope"/> to be acquired (<see cref="System.Threading.Timeout.Infinite"/> to wait indefinitely).</param> /// <param name="syncScope">If successful, returns the <see cref="ISyncScope"/> that was acquired.</param> /// <returns> /// True if the <see cref="ISyncScope"/> was acquired within the specified timeout (and returned in <paramref name="syncScope"/>); /// False if the <paramref name="timeout"/> elapsed before acquisition was possible (and <paramref name="syncScope"/> is set to null). /// </returns> public bool TryAcquire(object syncRoot, int timeout, out ISyncScope syncScope) { var context = CreateContext(); GC.SuppressFinalize(context); return(context.TryAcquire(syncRoot, timeout, out syncScope)); }