private void CheckCombination(PotentialCombination pc, ResultSet rs) { rs.Id = pc.CombinationId; foreach (var k in pc.SellerIds) { Seller seller = _sellers[k]; rs.Sellers.Add(seller.Storename); foreach (var lot in seller.AvailableItems) { rs.CurrentStatus[lot.GetId()] += lot.Quantity; rs.CurrentItems.Add(lot); } } if (rs.IsComplete()) { decimal cost = rs.CalculateCost(); // if less than the max number of results, add it to the results. If more than max number, only add if cost is // less than current max cost if (_results[pc.SellerCount].Count < _maxResults) { if (cost > _maxCost) { lock (_costLock) { _maxCost = cost; } } pc.Price = cost; pc.Id = rs.Id; AddResult(pc.SellerCount, pc); } else { if (cost < _maxCost) { pc.Price = cost; pc.Id = rs.Id; AddResult(pc.SellerCount, pc); } } } }
// allow results to be added to the results collection from multiple threads private void AddResult(int combination, PotentialCombination result) { lock (_resultLock) { _results[combination].Add(result); } }