protected async override Task ExecuteAsync(CancellationToken cancellationToken) { messageSource.Start(processAsync); try { await Task.Delay(Timeout.Infinite, cancellationToken); } catch (TaskCanceledException) { messageSource.Stop(); // wait for all consumer tasks to finish // this is important: a consumer task is only idempotent if does not get canceled while (consumerTasksPending != 0) { logger.LogInformation($"Waiting for {consumerTasksPending} Tasks to finish."); await Task.Delay(25); } } }