public void Update(DateTime time, int usages) { ExceededThreshold = null; foreach (var intervalThreshold in settings.IntervalThresholds) { var interval = (int)intervalThreshold.Key; var threshold = intervalThreshold.Value; if (!UsageByInterval.ContainsKey(interval)) { UsageByInterval.Add(interval, new UsageBeginAndCount(time)); } var current = UsageByInterval[interval]; var elapsed = time.Subtract(current.Begin).TotalSeconds; if (elapsed < interval) { current.Count += usages; } else { current.Begin = time; current.Count = usages; } if (current.Count > threshold) { ExceededThreshold = new Threshold { UsageInterval = TimeSpan.FromSeconds(interval), Usages = threshold, IsEndingIn = current.Begin + TimeSpan.FromSeconds(interval) - DateTime.Now }; } } }