public object Clone() { id = counter++; var res = new ProblemResult(); res.Time = Time; res.Songs = new List <Track>(Songs); return(res); }
private void Test(int part, ProblemResult res) { if (Results.Count < MaxResultCount || Results[0].Time < TotalSecunds) { if (res.Time + Tracks[part].TotalSecunds <= TotalSecunds) { var resCopy = (ProblemResult)res.Clone(); if (!IsLast(part)) { lock (lobj) { TaskCounter++; Task t1 = Task.Run(() => Test(part + 1, resCopy)); } } res.Time += Tracks[part].TotalSecunds; res.Songs.Add(Tracks[part]); if (!IsLast(part) && res.Time < TotalSecunds) { lock (lobj) { TaskCounter++; Task t2 = Task.Run(() => Test(part + 1, res)); } } else { TryAddResult(res); } } else { TryAddResult(res); } } lock (lobj) { TaskCounter--; try { if (TaskCounter == 0) { Completition.SetResult(Results); } } catch (Exception ex) { } } }
private void TryAddResult(ProblemResult res) { if (res.Time > MaxResultTime) { MaxResultTime = res.Time; Results.Clear(); Results.Add(res); } else if (res.Time == MaxResultTime) { Results.Add(res); } }