I am adding background tasks to the lock assembly (added in the background).
I am waiting with Task.WhenAll in the Enumerable returned by GetConsumingEnumerable.
My question is: is Task.WhenAll overloaded, which gets IEnumerable "prepared" to potentially get an infinite number of tasks?
I'm just not sure if I can do it this way or if it is intended to be used that way?
private async Task RunAsync(TimeSpan delay, CancellationToken cancellationToken) { using (BlockingCollection<Task> jobcollection = new BlockingCollection<Task>()) { Task addingTask = Task.Run(async () => { while (true) { DateTime utcNow = DateTime.UtcNow; var jobs = Repository.GetAllJobs(); foreach (var job in GetRootJobsDue(jobs, utcNow)) { jobcollection.Add(Task.Run(() => RunJob(job, jobs, cancellationToken, utcNow), cancellationToken), cancellationToken); } await Task.Delay(delay, cancellationToken); } }, cancellationToken); await Task.WhenAll(jobcollection.GetConsumingEnumerable(cancellationToken)); } }
source share