|
using System;
using System.Timers;
using System.ComponentModel;
namespace CamAlarm
{
public class cTimer : IDisposable
{
#region Events
public delegate void CompleteDelegate(object sender);
public event CompleteDelegate Complete;
public delegate void TickDelegate(object sender);
public event TickDelegate Tick;
#endregion
#region Fields
private bool _bCancelTimer;
private bool _bIsReset;
private long _lTickCounter;
private long _lTickMaximum;
private System.Timers.Timer _aTimer;
#endregion
#region Properties
public bool Cancel
{
get { return _bCancelTimer; }
set { _bCancelTimer = value; }
}
public bool Enabled
{
get { return _aTimer.Enabled; }
set { _aTimer.Enabled = value; }
}
public double Interval
{
get { return _aTimer.Interval; }
set { _aTimer.Interval = value; }
}
public long TickCount
{
get { return _lTickCounter; }
set { _lTickCounter = value; }
}
public long TickMaximum
{
get { return _lTickMaximum; }
set { _lTickMaximum = value; }
}
#endregion
#region Constructor
public cTimer(object sender)
{
this.TickCount = 0;
this.TickMaximum = 1000;
_aTimer = new System.Timers.Timer();
_aTimer.SynchronizingObject = (ISynchronizeInvoke)sender;
_aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
this.Interval = 1000;
}
#endregion
#region Methods
private void Reset()
{
this.Enabled = false;
this.TickCount = 0;
this.Cancel = false;
}
public void Dispose()
{
Stop();
_aTimer.Dispose();
GC.SuppressFinalize(this);
}
public void Start(double interval, long maximum)
{
this.Interval = interval;
this.TickMaximum = maximum;
this.Enabled = true;
}
public void Stop()
{
Reset();
}
#endregion
#region Event Handlers
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
if ((this.TickCount > this.TickMaximum && this.TickMaximum != -1) || this.Cancel)
{
Reset();
if (Complete != null)
Complete(this);
}
else
{
if (this.TickMaximum != -1)
this.TickCount++;
if (Tick != null)
Tick(this);
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Network and programming specialist. Started in C, and have learned about 14 languages since then. Cisco programmer, and lately writing a lot of C# and WPF code, (learning Java too). If I can dream it up, I can probably put it to code. My software company, (VTDev), is on the verge of releasing a couple of very cool things.. keep you posted.