#region --------------- Copyright Dresser Wayne Pignone -------------
/*
* $Log: /Wrk/WayneLibraries/Wrk/StateEngine/Timer.cs $
*
* 4 08-03-18 11:09 roger.månsson
* Each timer does not have it's own real timer anymore. Instead there are
* a pool of timers in the StateMachine that can be shared among the
* timers in the same state machine.
*
* 3 08-02-26 14:11 Mattias.larsson
* Removed Obsolete method.
*/
#endregion
#region Old file header
/*===============================================================================
* Timer
*
* Change history
* When Who Comment
* ---------- ------ ------------------------------------
* 2006-06-17 RMa Added userToken to the timer and timer event.
* 2006-05-09 RMa Improved dispose handling.
* 2006-03-02 RMa FXCop updates: Implement IDisposable.
* 2005-12-09 RMa Improved the Disable functionality.
* 2005-12-05 RMa This header added.
*
---------------------------------------------------------------------------------*/
#endregion
using System;
namespace Wayne.Lib.StateEngine
{
///
/// Delegate that is used to signal to the state machine that the timer event has timed out.
///
///
internal delegate void TimerElapsedDelegate(Timer elapsedTimer);
///
/// Timer is a timer that should be used within the state machine.
///
public class Timer : IDisposable
{
#region Fields
private bool isPeriodic;
private bool clearAtStateExit = true;
private object eventType;
private State ownerState;
private int interval;
private object userToken;
private bool enabled;
private int idx = nextIdx++;
//public int Idx
//{
// get { return idx; }
// set { idx = value; }
//}
#endregion
#region Static Fiedls
static int nextIdx;
#endregion
#region Events
internal event EventHandler OnEnable;
internal event EventHandler OnDisable;
#endregion
#region Construction
///
/// Constructor for Timer
///
/// State that created the timer
/// Type that should be set in the timer event that is sent when the timer fires
/// Interval of the timer
/// Token object that is supplied by the user and that is returned in the TimerEvent.
public Timer(State ownerState, object eventType, int interval, object userToken)
{
this.ownerState = ownerState;
this.eventType = eventType;
this.interval = interval;
this.userToken = userToken;
}
///
/// Finalizer
///
~Timer()
{
Dispose(false);
}
#endregion
#region Properties
///
/// Specifies if the timer should fire several times.
///
public bool IsPeriodic
{
get { return isPeriodic; }
set { isPeriodic = value; }
}
///
/// Specifies if the timer should be disabled automatically when the owning state is exit.
///
public bool ClearAtStateExit
{
get { return clearAtStateExit; }
set { clearAtStateExit = value; }
}
///
/// State object that created the timer.
///
public State OwnerState
{
get { return ownerState; }
set { ownerState = value; }
}
///
/// Event type that should be set in the TimerEvent when it fires.
///
public object EventType
{
get { return eventType; }
set { eventType = value; }
}
///
/// If the timer is active.
///
public bool Enabled
{
get { return enabled; }
}
///
/// User-supplied token that is sent in to the constructor at creation time.
///
public object UserToken
{
get { return userToken; }
}
///
/// The interval of the timer.
///
public int Interval
{
get { return interval; }
set { interval = value; }
}
#endregion
#region Public Methods
///
/// Enables the timer
///
public void Enable()
{
enabled = true;
if (OnEnable != null)
OnEnable(this, EventArgs.Empty);
}
///
/// Disables the timer.
///
public void Disable()
{
enabled = false;
if (OnDisable != null)
OnDisable(this, EventArgs.Empty);
}
#endregion
#region IDisposable Members
///
/// Dispose disables the timer.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (enabled)
Disable(); //Disable the timer.
if (disposing)
{
//Nothing to dispose.
}
}
#endregion
#region ToString
///
/// ToString method.
///
///
public override string ToString()
{
return string.Format(System.Globalization.CultureInfo.InvariantCulture, "Timer State={0}, Idx={1}, Interval={2}, IsPeriodic={3}", ownerState.LogName, idx, interval, IsPeriodic);
}
#endregion
}
}