public IEnumerable <T> AsEnumerable(CancellationToken cancellationToken = null, IJobLogger logger = null) { if (cancellationToken == null) { cancellationToken = new CancellationToken(); } var errorHandler = new ErrorHandler(); Job <T>[] jobs = Enumerable.Range(1, jobCount) .Select(i => new Job <T>($"{i}", enumeration, cancellationToken)) .ToArray(); while (true) { if (HandleErrors(jobs, errorHandler)) { cancellationToken.Cancel(); break; } if (logger != null) { logger.LogResultsCount(jobs.Select(job => job.ResultsCount).Sum()); } List <T> results = new List <T>(jobs.Length); foreach (var job in jobs) { if (job.TryGetResult(out T result)) { results.Add(result); } } foreach (T result in results) { yield return(result); } if (jobs.All(job => job.IsFinished)) { HandleErrors(jobs, errorHandler); break; } else { Thread.Sleep(5); } } foreach (var job in jobs) { job.Dispose(); } errorHandler.ThrowIfFailed(); }