public void Add(int value) { Total += value; if (Total < Threshold) { return; } ThresholdEventArgs t = new ThresholdEventArgs() { Value = Threshold, TimeReached = DateTime.Now }; OnThresholdreached(t); }
//add a number to total, if number is greater than threshold, runs an event. public void Add(int i) { total += i; ThresholdEventArgs args = new ThresholdEventArgs(); args.WhenHappened = DateTime.Now; args.ValueGreaterThanThreshold = total; if (total > THRESHOLD) { //runs event handler statements this.OnThresholdReachedEventHandler?.Invoke(this, args);//suggested syntax } else { //runs event handler statements EventHandler <ThresholdEventArgs> notReachedHandler = this.OnThresholdNotReachedEventHandler;//old syntax if (notReachedHandler != null) { notReachedHandler(this, args); } } }
//when threshold is not reached, prints a message just for info private static void counter_OnThresholdNotReachedEventHandler(object sender, ThresholdEventArgs e) { //here we have event handler statements Console.WriteLine($"INFO: Threshold not reached at {e.WhenHappened}: Total is {e.ValueGreaterThanThreshold}."); }
private void OnThresholdreached(ThresholdEventArgs threshold) { ThresholdReached?.Invoke(this, threshold); }
private static void c_Thresholdreached(object sender, ThresholdEventArgs e) { Console.WriteLine($"Threshold value {e.Value} at time {e.TimeReached}."); }