Example #1
0
        private void ZReleaseToken(cTrace.cContext pParentContext)
        {
            var lContext = pParentContext.NewMethod(nameof(cExclusiveAccess), nameof(ZReleaseToken), mName, mInstance);

            if (mDisposed)
            {
                throw new ObjectDisposedException(nameof(cExclusiveAccess));
            }
            mToken = null;
            Released?.Invoke(lContext);
            mExclusiveSemaphoreSlim.Release();
        }
Example #2
0
        /// <summary>
        /// Gets a disposable object that represents a grant of exclusive access.
        /// This method will not complete until the exclusive access is granted or it throws due to <see cref="cMethodControl"/>.
        /// Dispose the object to release the exclusive access.
        /// </summary>
        /// <param name="pMC">Controls the execution of the method.</param>
        /// <param name="pParentContext">Context for trace messages.</param>
        /// <returns></returns>
        public async Task <cToken> GetTokenAsync(cMethodControl pMC, cTrace.cContext pParentContext)
        {
            var lContext = pParentContext.NewMethod(nameof(cExclusiveAccess), nameof(GetTokenAsync), mName, mInstance);

            if (mDisposed)
            {
                throw new ObjectDisposedException(nameof(cExclusiveAccess));
            }
            if (!await mExclusiveSemaphoreSlim.WaitAsync(pMC.Timeout, pMC.CancellationToken).ConfigureAwait(false))
            {
                throw new TimeoutException();
            }
            while (mBlocks > 0)
            {
                if (!await mBlockCheckSemaphoreSlim.WaitAsync(pMC.Timeout, pMC.CancellationToken).ConfigureAwait(false))
                {
                    throw new TimeoutException();
                }
            }
            mToken = new cToken(mName, mSequence, mInstance, ZReleaseToken, pParentContext);
            return(mToken);
        }