Esempio n. 1
0
        public async Task <Message> ReceiveAsync(CancellationToken token)
        {
            bool lockAcquired = false;

            try
            {
                await sourceLock.WaitAsync(token);

                lockAcquired = true;
                return(await source.ReceiveAsync(token));
            }
            catch (OperationCanceledException)
            {
                // TODO: Fix the timeout value reported
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(
                                                                              SR.Format(SR.ReceiveTimedOut2, TimeSpan.Zero),
                                                                              TimeoutHelper.CreateEnterTimedOutException(TimeSpan.Zero)));
            }
            finally
            {
                if (lockAcquired)
                {
                    sourceLock.Release();
                }
            }
        }
        public async Task <Message> ReceiveAsync(TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            // If timeout == TimeSpan.MaxValue, then we want to pass Timeout.Infinite as
            // SemaphoreSlim doesn't accept timeouts > Int32.MaxValue.
            // Using TimeoutHelper.RemainingTime() would yield a value less than TimeSpan.MaxValue
            // and would result in the value Int32.MaxValue so we must use the original timeout specified.
            if (!await _sourceLock.WaitAsync(TimeoutHelper.ToMilliseconds(timeout)))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          new TimeoutException(SR.Format(SR.ReceiveTimedOut2, timeout),
                                               TimeoutHelper.CreateEnterTimedOutException(timeout)));
            }

            try
            {
                return(await _source.ReceiveAsync(timeoutHelper.RemainingTime()));
            }
            finally
            {
                _sourceLock.Release();
            }
        }