Timer.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #region --------------- Copyright Dresser Wayne Pignone -------------
  2. /*
  3. * $Log: /Wrk/WayneLibraries/Wrk/StateEngine/Timer.cs $
  4. *
  5. * 4 08-03-18 11:09 roger.månsson
  6. * Each timer does not have it's own real timer anymore. Instead there are
  7. * a pool of timers in the StateMachine that can be shared among the
  8. * timers in the same state machine.
  9. *
  10. * 3 08-02-26 14:11 Mattias.larsson
  11. * Removed Obsolete method.
  12. */
  13. #endregion
  14. #region Old file header
  15. /*===============================================================================
  16. * Timer
  17. *
  18. * Change history
  19. * When Who Comment
  20. * ---------- ------ ------------------------------------
  21. * 2006-06-17 RMa Added userToken to the timer and timer event.
  22. * 2006-05-09 RMa Improved dispose handling.
  23. * 2006-03-02 RMa FXCop updates: Implement IDisposable.
  24. * 2005-12-09 RMa Improved the Disable functionality.
  25. * 2005-12-05 RMa This header added.
  26. *
  27. ---------------------------------------------------------------------------------*/
  28. #endregion
  29. using System;
  30. namespace Wayne.Lib.StateEngine
  31. {
  32. /// <summary>
  33. /// Delegate that is used to signal to the state machine that the timer event has timed out.
  34. /// </summary>
  35. /// <param name="elapsedTimer"></param>
  36. internal delegate void TimerElapsedDelegate(Timer elapsedTimer);
  37. /// <summary>
  38. /// Timer is a timer that should be used within the state machine.
  39. /// </summary>
  40. public class Timer : IDisposable
  41. {
  42. #region Fields
  43. private bool isPeriodic;
  44. private bool clearAtStateExit = true;
  45. private object eventType;
  46. private State ownerState;
  47. private int interval;
  48. private object userToken;
  49. private bool enabled;
  50. private int idx = nextIdx++;
  51. //public int Idx
  52. //{
  53. // get { return idx; }
  54. // set { idx = value; }
  55. //}
  56. #endregion
  57. #region Static Fiedls
  58. static int nextIdx;
  59. #endregion
  60. #region Events
  61. internal event EventHandler OnEnable;
  62. internal event EventHandler OnDisable;
  63. #endregion
  64. #region Construction
  65. /// <summary>
  66. /// Constructor for Timer
  67. /// </summary>
  68. /// <param name="ownerState">State that created the timer</param>
  69. /// <param name="eventType">Type that should be set in the timer event that is sent when the timer fires</param>
  70. /// <param name="interval">Interval of the timer</param>
  71. /// <param name="userToken">Token object that is supplied by the user and that is returned in the TimerEvent.</param>
  72. public Timer(State ownerState, object eventType, int interval, object userToken)
  73. {
  74. this.ownerState = ownerState;
  75. this.eventType = eventType;
  76. this.interval = interval;
  77. this.userToken = userToken;
  78. }
  79. /// <summary>
  80. /// Finalizer
  81. /// </summary>
  82. ~Timer()
  83. {
  84. Dispose(false);
  85. }
  86. #endregion
  87. #region Properties
  88. /// <summary>
  89. /// Specifies if the timer should fire several times.
  90. /// </summary>
  91. public bool IsPeriodic
  92. {
  93. get { return isPeriodic; }
  94. set { isPeriodic = value; }
  95. }
  96. /// <summary>
  97. /// Specifies if the timer should be disabled automatically when the owning state is exit.
  98. /// </summary>
  99. public bool ClearAtStateExit
  100. {
  101. get { return clearAtStateExit; }
  102. set { clearAtStateExit = value; }
  103. }
  104. /// <summary>
  105. /// State object that created the timer.
  106. /// </summary>
  107. public State OwnerState
  108. {
  109. get { return ownerState; }
  110. set { ownerState = value; }
  111. }
  112. /// <summary>
  113. /// Event type that should be set in the TimerEvent when it fires.
  114. /// </summary>
  115. public object EventType
  116. {
  117. get { return eventType; }
  118. set { eventType = value; }
  119. }
  120. /// <summary>
  121. /// If the timer is active.
  122. /// </summary>
  123. public bool Enabled
  124. {
  125. get { return enabled; }
  126. }
  127. /// <summary>
  128. /// User-supplied token that is sent in to the constructor at creation time.
  129. /// </summary>
  130. public object UserToken
  131. {
  132. get { return userToken; }
  133. }
  134. /// <summary>
  135. /// The interval of the timer.
  136. /// </summary>
  137. public int Interval
  138. {
  139. get { return interval; }
  140. set { interval = value; }
  141. }
  142. #endregion
  143. #region Public Methods
  144. /// <summary>
  145. /// Enables the timer
  146. /// </summary>
  147. public void Enable()
  148. {
  149. enabled = true;
  150. if (OnEnable != null)
  151. OnEnable(this, EventArgs.Empty);
  152. }
  153. /// <summary>
  154. /// Disables the timer.
  155. /// </summary>
  156. public void Disable()
  157. {
  158. enabled = false;
  159. if (OnDisable != null)
  160. OnDisable(this, EventArgs.Empty);
  161. }
  162. #endregion
  163. #region IDisposable Members
  164. /// <summary>
  165. /// Dispose disables the timer.
  166. /// </summary>
  167. public void Dispose()
  168. {
  169. Dispose(true);
  170. GC.SuppressFinalize(this);
  171. }
  172. private void Dispose(bool disposing)
  173. {
  174. if (enabled)
  175. Disable(); //Disable the timer.
  176. if (disposing)
  177. {
  178. //Nothing to dispose.
  179. }
  180. }
  181. #endregion
  182. #region ToString
  183. /// <summary>
  184. /// ToString method.
  185. /// </summary>
  186. /// <returns></returns>
  187. public override string ToString()
  188. {
  189. return string.Format(System.Globalization.CultureInfo.InvariantCulture, "Timer State={0}, Idx={1}, Interval={2}, IsPeriodic={3}", ownerState.LogName, idx, interval, IsPeriodic);
  190. }
  191. #endregion
  192. }
  193. }