using System; using System.Timers; namespace Chernobyl.Switch { /// /// An that repeatedly invokes an event when it is on /// and stops invoking that event when it is off. This class is similar to /// the class but this class has event handler methods /// that can be assigned to events. /// public class Repeater : ISwitch { /// /// Initializes a new instance of the class. /// /// The rate, in milliseconds, /// at which the event will be fired. public Repeater(int repeatRateInMilliseconds) { RepeatRateInMilliseconds = repeatRateInMilliseconds; } /// /// An event that is raised at the rate specified by /// . The invocation only happens /// if one of the Start methods has been called ( /// and ). /// public event EventHandler Elapsed; /// /// Starts the repeated invoking of the event at /// the repeat rate specified by . /// public void Start() { if(Timer.Enabled != true) { Timer.Start(); if (SwitchedOn != null) SwitchedOn(this, EventArgs.Empty); } } /// /// An event handler that can be assigned to an event. When invoked, /// this method does the same thing as . /// /// The sender. /// The instance /// containing the event data. public void Start(object sender, EventArgs e) { Start(); } /// /// Stops the repeated invoking of the event. /// public void Stop() { if(Timer.Enabled != false) { Timer.Stop(); if (SwitchedOff != null) SwitchedOff(this, EventArgs.Empty); } } /// /// An event handler that can be assigned to an event. When invoked, /// this method does the same thing as . /// /// The sender. /// The instance /// containing the event data. public void Stop(object sender, EventArgs e) { Stop(); } /// /// An event that is raised when this switch has been started. /// public event EventHandler SwitchedOn; /// /// An event that is raised right after this switch has been stopped. /// public event EventHandler SwitchedOff; /// /// Resets the switch to its original state or the its state after right /// after its creation. Calling this method is the same as calling /// /// public void Reset() { Stop(); } /// /// An event handler that can be assigned to an event. This event handler /// will reset the switch to its original state. It is exactly the same /// as calling . /// /// The sender of the event. /// The arguments to the event. public void Reset(object sender, EventArgs e) { Reset(); } /// /// The rate, in milliseconds, at which the event /// will be fired. /// public int RepeatRateInMilliseconds { get { return _RepeatRateInMilliseconds; } set { _RepeatRateInMilliseconds = value; bool enabled = false; if (Timer != null) { enabled = Timer.Enabled; Timer.Elapsed -= Timer_Elapsed; } Timer = new Timer(_RepeatRateInMilliseconds); Timer.Elapsed += Timer_Elapsed; if(enabled == true) Timer.Start(); } } /// /// Handles the Elapsed event of the Timer control. This method invokes /// the event on this class. /// /// The source of the event. /// The /// instance containing the event data. void Timer_Elapsed(object sender, ElapsedEventArgs e) { if (Elapsed != null) Elapsed(this, e); } /// /// The which is used to invoke the /// event at a specified rate. /// Timer Timer { get; set; } /// /// The backing field to /// int _RepeatRateInMilliseconds; } }