public static IEnumerable <IStorageQueueMessage> GetMessages(this IStorageQueue queue, int messageCount)
        {
            if (queue == null)
            {
                throw new ArgumentNullException("queue");
            }

            return(queue.GetMessagesAsync(messageCount, visibilityTimeout: null, options: null, operationContext: null,
                                          cancellationToken: CancellationToken.None).GetAwaiter().GetResult());
        }
        public async Task <TaskSeriesCommandResult> ExecuteAsync(CancellationToken cancellationToken)
        {
            lock (_stopWaitingTaskSourceLock)
            {
                if (_stopWaitingTaskSource != null)
                {
                    _stopWaitingTaskSource.TrySetResult(null);
                }

                _stopWaitingTaskSource = new TaskCompletionSource <object>();
            }

            IEnumerable <IStorageQueueMessage> batch;

            try
            {
                batch = await _queue.GetMessagesAsync(_queueProcessor.BatchSize,
                                                      _visibilityTimeout,
                                                      options : null,
                                                      operationContext : null,
                                                      cancellationToken : cancellationToken);
            }
            catch (StorageException exception)
            {
                if (exception.IsNotFoundQueueNotFound() ||
                    exception.IsConflictQueueBeingDeletedOrDisabled() ||
                    exception.IsServerSideError())
                {
                    // Back off when no message is available, or when
                    // transient errors occur
                    return(CreateBackoffResult());
                }
                else
                {
                    throw;
                }
            }

            if (batch == null)
            {
                return(CreateBackoffResult());
            }

            bool foundMessage = false;

            foreach (var message in batch)
            {
                if (message == null)
                {
                    continue;
                }

                foundMessage = true;

                // Note: Capturing the cancellationToken passed here on a task that continues to run is a slight abuse
                // of the cancellation token contract. However, the timer implementation would not dispose of the
                // cancellation token source until it has stopped and perhaps also disposed, and we wait for all
                // outstanding tasks to complete before stopping the timer.
                Task task = ProcessMessageAsync(message, _visibilityTimeout, cancellationToken);

                // Having both WaitForNewBatchThreshold and this method mutate _processing is safe because the timer
                // contract is serial: it only calls ExecuteAsync once the wait expires (and the wait won't expire until
                // WaitForNewBatchThreshold has finished mutating _processing).
                _processing.Add(task);
            }

            // Back off when no message was found.
            if (!foundMessage)
            {
                return(CreateBackoffResult());
            }

            _foundMessageSinceLastDelay = true;
            return(CreateSucceededResult());
        }