Example #1
0
        private async void PeriodicallyExecute()
        {
            while (true)
            {
                bool disposeWasRequested;
                try
                {
                    await Task.Delay(Interval, _disposeCts.Token).ConfigureAwait(false);

                    disposeWasRequested = false;
                }
                catch (OperationCanceledException)
                {
                    disposeWasRequested = true;
                }

                if (_queue.TryDequeue(out var firstItem))
                {
                    var items = ArrayUtils.ArrayFromDequeuing(firstItem, _queue);

                    IServiceScope serviceScope;
                    try
                    {
                        serviceScope = ServiceProvider.CreateScope();
                    }
                    catch
                    {
                        break;
                    }

                    try
                    {
                        await ExecuteAsync(items, serviceScope.ServiceProvider).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        await HandleExceptionWithRetryAsync(ex, items, serviceScope.ServiceProvider).ConfigureAwait(false);
                    }
                    finally
                    {
                        serviceScope.Dispose();
                    }
                }

                if (disposeWasRequested)
                {
                    break;
                }
            }
        }