public void Add(int x) { total += x; if (total > mThreshold) { ThresholdReachedEventArgs args = new ThresholdReachedEventArgs { Threshold = mThreshold, TimeReached = DateTime.Now }; OnThresholdReached(args);//invoke event handler } }
protected virtual void OnThresholdReached(ThresholdReachedEventArgs e) { EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached; if (handler != null) { Array.ForEach<Delegate>(handler.GetInvocationList(), (item) => { try { item.DynamicInvoke(this, e); } catch (Exception ex) { Console.WriteLine(ex.Message); } }); //handler(this, e); } }
static void counter_Threshold2(object sender, ThresholdReachedEventArgs e) { Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached); }
static void counter_Threshold(object sender, ThresholdReachedEventArgs e) { Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached); throw new ArgumentException("e"); }