void DoConsume() { tokenSource = new CancellationTokenSource(); task = Task.Factory.StartNew(async token => { var cancellationToken = (CancellationToken)token; while (!cancellationToken.IsCancellationRequested) { EventWrapper result = default; try { result = await eventStore.PopAsync(); if (result is null) { await Task.Delay(TimeSpan.FromSeconds(1)); continue; } } catch (Exception e) { logger.LogError($"Error occured: {e.Message}"); continue; } logger.LogInformation($"Consume message '{result.Name}:{result.Message}'."); var eventName = result.Name; var eventMessage = result.Message; await ProcessEventAsync(eventName, eventMessage); } }, tokenSource.Token); }