123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #region --------------- Copyright Dresser Wayne Pignone -------------
- /*
- * $Log: /Wrk/WayneLibraries/Wrk/StateEngine/TimerWrapper.cs $
- *
- * 1 08-03-18 11:07 roger.månsson
- * Created.
- */
- #endregion
- using System;
- using System.Threading;
- namespace Wayne.Lib.StateEngine
- {
- /// <summary>
- /// Wraps a System.Threading.Timer together with an optional state engine timer. It is used
- /// to make it possible to reuse the Timer object, since it is quite expensive to create and
- /// destroy timers all the time. Now we only change the intervals on the timers, and disable them
- /// when they should not be active anymore.
- /// </summary>
- class TimerWrapper : IDisposable
- {
- private readonly TimerCallback timerCallback;
- #region Fields
- StateEngine.Timer stateEngineTimer;
- System.Threading.Timer realTimer;
- #endregion
- #region Construction
- /// <summary>
- /// Initializes a new instance of the TimerWrapper class.
- /// </summary>
- /// <param name="timerCallback">Delegate that should be invoked when timer fires.</param>
- public TimerWrapper(TimerCallback timerCallback)
- {
- this.timerCallback = timerCallback;
- realTimer = new System.Threading.Timer(timerCallback, this, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
- }
- /// <summary>
- /// Finalizer
- /// </summary>
- ~TimerWrapper()
- {
- Dispose(false);
- }
- #endregion
- #region Properties
- /// <summary>
- /// Gets or sets the State Engine timer.
- /// </summary>
- public StateEngine.Timer StateEngineTimer
- {
- get { return stateEngineTimer; }
- set { stateEngineTimer = value; }
- }
- #endregion
- #region Methods
- /// <summary>
- /// Disables the underlying timer.
- /// </summary>
- internal void Disable()
- {
- realTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
- }
- /// <summary>
- /// Enables the underlying timer.
- /// </summary>
- internal void Enable()
- {
- if (stateEngineTimer == null)
- throw new InvalidOperationException("Can not enable timer when no StateEngine timer is connected.");
- if (stateEngineTimer.IsPeriodic)
- realTimer.Change(stateEngineTimer.Interval, stateEngineTimer.Interval);
- else
- realTimer.Change(stateEngineTimer.Interval, System.Threading.Timeout.Infinite);
- }
- /// <summary>
- /// Fires the timer wrapper immediately.
- /// </summary>
- public void Fire()
- {
- if (timerCallback != null)
- timerCallback(this);
- }
- #endregion
- #region IDisposable Members
- /// <summary>
- /// Disposes the unmanaged resources
- /// </summary>
- /// <param name="disposing"></param>
- protected virtual void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (realTimer != null)
- {
- realTimer.Dispose();
- }
- realTimer = null;
- }
- }
- /// <summary>
- /// Disposes the unmanaged resources
- /// </summary>
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- #endregion
- }
- }
|