TimerWrapper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #region --------------- Copyright Dresser Wayne Pignone -------------
  2. /*
  3. * $Log: /Wrk/WayneLibraries/Wrk/StateEngine/TimerWrapper.cs $
  4. *
  5. * 1 08-03-18 11:07 roger.månsson
  6. * Created.
  7. */
  8. #endregion
  9. using System;
  10. using System.Threading;
  11. namespace Wayne.Lib.StateEngine
  12. {
  13. /// <summary>
  14. /// Wraps a System.Threading.Timer together with an optional state engine timer. It is used
  15. /// to make it possible to reuse the Timer object, since it is quite expensive to create and
  16. /// destroy timers all the time. Now we only change the intervals on the timers, and disable them
  17. /// when they should not be active anymore.
  18. /// </summary>
  19. class TimerWrapper : IDisposable
  20. {
  21. private readonly TimerCallback timerCallback;
  22. #region Fields
  23. StateEngine.Timer stateEngineTimer;
  24. System.Threading.Timer realTimer;
  25. #endregion
  26. #region Construction
  27. /// <summary>
  28. /// Initializes a new instance of the TimerWrapper class.
  29. /// </summary>
  30. /// <param name="timerCallback">Delegate that should be invoked when timer fires.</param>
  31. public TimerWrapper(TimerCallback timerCallback)
  32. {
  33. this.timerCallback = timerCallback;
  34. realTimer = new System.Threading.Timer(timerCallback, this, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
  35. }
  36. /// <summary>
  37. /// Finalizer
  38. /// </summary>
  39. ~TimerWrapper()
  40. {
  41. Dispose(false);
  42. }
  43. #endregion
  44. #region Properties
  45. /// <summary>
  46. /// Gets or sets the State Engine timer.
  47. /// </summary>
  48. public StateEngine.Timer StateEngineTimer
  49. {
  50. get { return stateEngineTimer; }
  51. set { stateEngineTimer = value; }
  52. }
  53. #endregion
  54. #region Methods
  55. /// <summary>
  56. /// Disables the underlying timer.
  57. /// </summary>
  58. internal void Disable()
  59. {
  60. realTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
  61. }
  62. /// <summary>
  63. /// Enables the underlying timer.
  64. /// </summary>
  65. internal void Enable()
  66. {
  67. if (stateEngineTimer == null)
  68. throw new InvalidOperationException("Can not enable timer when no StateEngine timer is connected.");
  69. if (stateEngineTimer.IsPeriodic)
  70. realTimer.Change(stateEngineTimer.Interval, stateEngineTimer.Interval);
  71. else
  72. realTimer.Change(stateEngineTimer.Interval, System.Threading.Timeout.Infinite);
  73. }
  74. /// <summary>
  75. /// Fires the timer wrapper immediately.
  76. /// </summary>
  77. public void Fire()
  78. {
  79. if (timerCallback != null)
  80. timerCallback(this);
  81. }
  82. #endregion
  83. #region IDisposable Members
  84. /// <summary>
  85. /// Disposes the unmanaged resources
  86. /// </summary>
  87. /// <param name="disposing"></param>
  88. protected virtual void Dispose(bool disposing)
  89. {
  90. if (disposing)
  91. {
  92. if (realTimer != null)
  93. {
  94. realTimer.Dispose();
  95. }
  96. realTimer = null;
  97. }
  98. }
  99. /// <summary>
  100. /// Disposes the unmanaged resources
  101. /// </summary>
  102. public void Dispose()
  103. {
  104. Dispose(true);
  105. GC.SuppressFinalize(this);
  106. }
  107. #endregion
  108. }
  109. }