Example #1
0
        // Wrap event invocations inside a protected virtual method
        // to allow derived classes to override the event invocation behavior
        protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
        {
            // Make a temporary copy of the event to avoid possibility of
            // a race condition if the last subscriber unsubscribes
            // immediately after the null check and before the event is raised.
            EventHandler <ThresholdReachedEventArgs> thresholdReached = ThresholdReached;

            // Event will be null if there are no subscribers, checking for null
            // Call to raise the event.
            thresholdReached?.Invoke(this, e);
        }
Example #2
0
        public void Add(int x)
        {
            _total += x;
            if (_total < Threshold)
            {
                return;
            }
            var args = new ThresholdReachedEventArgs(Threshold, DateTime.Now);

            // Raising an event. You can also raise an event
            // before you execute a block of code.
            OnThresholdReached(args);
        }
Example #3
0
 // Define what actions to take when the event is raised.
 // This is EVENT HANDLER
 private static void Counter_ThresholdReached(object sender, ThresholdReachedEventArgs e)
 {
     Console.WriteLine($"The threshold of {e.Threshold} is reached at {e.TimeReached:F}");
     Environment.Exit(0);
 }