public void ExecuteTasks(CancellationToken token) { while (true) { if (_counter < _numberOfParallelTasks) { //Wait indefinitely until a segment is queued if (_segmentTaskQueue.GetQueue().TryTake(out SelfRefreshingSegment segment, -1)) { if (Log.IsDebugEnabled) { Log.Debug(string.Format("Segment dequeued: {0}", segment.Name)); } IncrementCounter(); Task task = new Task(async() => await segment.FetchSegment(), token); task.ContinueWith((x) => { DecrementCounter(); }); task.Start(); } } else { waitForExecution.WaitOne(); } } }
public void ExecuteTasks(CancellationToken token) { while (!token.IsCancellationRequested) { if (_counter < _numberOfParallelTasks) { try { //Wait indefinitely until a segment is queued if (_segmentTaskQueue.GetQueue().TryTake(out SelfRefreshingSegment segment, -1, token)) { if (Log.IsDebugEnabled) { Log.Debug(string.Format("Segment dequeued: {0}", segment.Name)); } if (!token.IsCancellationRequested) { IncrementCounter(); Task task = new Task(async() => await segment.FetchSegment(new FetchOptions()), token); task.ContinueWith((x) => { DecrementCounter(); }, token); task.Start(); } } } catch (Exception ex) { Log.Debug(ex.Message); } } else { waitForExecution.WaitOne(); } } }