private void ThreadBody(object obj) { while (!this._token.IsCancellationRequested) { foreach (var t in Tasks.Where(x => x.Retries > 0)) { if (t.RedPercent == 0) { t.Retries = 0; } } var e = ToWork.ToList(); if (e.Any()) { if (DnConf.Parallels == 1) { e.First().Start(); } else { e = e.Take(Math.Min(DnConf.Parallels, e.Count)).ToList(); var tl = new List <Task>(); foreach (var x in e) { var t = Task.Factory.StartNew(x.Start); tl.Add(t); } Task.WaitAll(tl.ToArray()); } } foreach (var task in CompletedTasks.Where(x => x.Delivered == false)) { task.Complete(); task.Delivered = true; } if (Tasks.Count() == Tasks.Count(x => x.Delivered)) { return; } //this._signalEvent.Reset(); //DnTask[] tasksSnapshot; //lock (this._locker) //{ // tasksSnapshot = this.Tasks.ToArray(); // if (tasksSnapshot.Length > 0 ) // tasksSnapshot[0].Begin(); //} //var res = WaitHandle.WaitAny(new[] {this._token.WaitHandle, this._signalEvent}); //switch (res) //{ // case -1: // break; // case 0: // break; // case 1: // break; //} Thread.Sleep(DnConf.Delay); } Console.WriteLine("DownloadService.ThreadBody exit"); }