using log4net; namespace TBF.BenchControl.Operations { /// /// Implements timer function /// /// Constructors: /// Timer(timerTimeMs) ................ use Event.TimerExpired as eventExpired /// Timer(timerTimeMs, eventExpired) .. use constructor argument as eventExpired /// /// Start argument: /// /// Events: /// Event.None ........ operation is in progress /// eventExpired ...... timer expired /// public class TimerOp : IOperation { private static readonly ILog log = LogManager.GetLogger(typeof(TimerOp)); public override string ToString() { return string.Format("TimerOp({0}s, Event.{1})", timerTime, eventExpired); } /// Private fields bool timeExpired = false; int expireTime; /// Set by constructors int timerTime; Event eventExpired; TBF.Boxes.IntBox remainingTimeSec; /// /// Constructors, optional argument is a timer name (you can create more then one timer) /// public TimerOp(int timerTimeSec, Event eventExpired, TBF.Boxes.IntBox remainingTimeSec) { this.timerTime = timerTimeSec; this.eventExpired = eventExpired; this.remainingTimeSec = remainingTimeSec; } public TimerOp(int timerTimeSec, Event eventExpired) : this(timerTimeSec, eventExpired, null) { } public TimerOp(int timerTime, TBF.Boxes.IntBox remainingTimeSec) : this(timerTime, Event.TimerExpired, remainingTimeSec) { } public TimerOp(int timerTime) : this(timerTime, Event.TimerExpired, null) { } void UpdateRemainingTime() { if (remainingTimeSec != null) { remainingTimeSec.Val = System.Math.Max(expireTime - StateMachine.Time, 0); } } /// Time period (TimeSpan) public void Start() { timeExpired = false; expireTime = StateMachine.Time + timerTime; UpdateRemainingTime(); } public Event Run() { UpdateRemainingTime(); if (timeExpired) { return eventExpired; } else if (StateMachine.Time >= expireTime) { timeExpired = true; return eventExpired; } else { return Event.TimerBusy; } } public void Stop() { timeExpired = false; } } }