예제 #1
0
        public async Task <bool> WaitForMessageAsync(CancellationToken token)
        {
            bool lockAcquired = false;

            try
            {
                await sourceLock.WaitAsync(token);

                lockAcquired = true;
                return(await source.WaitForMessageAsync(token));
            }
            catch (OperationCanceledException)
            {
                // TODO: Fix the timeout value reported
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(
                                                                              SR.Format(SR.WaitForMessageTimedOut, TimeSpan.Zero),
                                                                              TimeoutHelper.CreateEnterTimedOutException(TimeSpan.Zero)));
            }
            finally
            {
                if (lockAcquired)
                {
                    sourceLock.Release();
                }
            }
        }
        public async Task <bool> WaitForMessageAsync(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.WaitForMessageTimedOut, timeout),
                                               TimeoutHelper.CreateEnterTimedOutException(timeout)));
            }
            try
            {
                return(await _source.WaitForMessageAsync(timeoutHelper.RemainingTime()));
            }
            finally
            {
                _sourceLock.Release();
            }
        }