/*************************************************************************
*
* file : RepeatingTimer.cs
* copyright : (C) The WCell Team
* email : info@wcell.org
* last changed : $LastChangedDate: 2008-03-09 19:28:05 -0400 (Sun, 09 Mar 2008) $
* last author : $LastChangedBy: tobz $
* revision : $Rev: 183 $
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*************************************************************************/
using System;
namespace WCell.Core.Timers
{
///
/// A repeating timer that will continue to fire after its first
/// tick until stopped manually.
///
public class RepeatingTimer : ITimer
{
private volatile bool m_running;
private long m_delay, m_interval, m_started, m_lastTick;
private TimerCallback m_callback;
///
/// Default constructor.
///
/// the timer callback
public RepeatingTimer(TimerCallback callback)
{
m_callback = callback;
}
///
/// Whether or not the timer is running.
///
public bool IsRunning
{
get
{
return m_running;
}
}
///
/// Starts the timer, given the initial firing delay.
///
/// the time before the timer fires in milliseconds
/// the time between ticks after the initial tick in millisecond
public void Start(int delay, int interval)
{
Start((long)delay, (long)interval);
}
///
/// Starts the timer, given the initial firing delay.
///
/// the time before the timer fires in milliseconds
/// the time between ticks after the initial tick in millisecond
public void Start(uint delay, uint interval)
{
Start((long)delay, (long)interval);
}
///
/// Starts the timer, given the initial firing delay.
///
/// the time before the timer fires in milliseconds
/// the time between ticks after the initial tick in millisecond
public void Start(long delay, long interval)
{
m_delay = delay;
m_interval = interval;
m_started = m_lastTick = Utility.GetSystemTimeLong();
m_running = true;
}
///
/// Starts the timer, given the initial firing delay.
///
/// the time before the timer fires in TimeSpan format
/// the time between ticks after the initial tick in TimeSpan format
public void Start(TimeSpan delay, TimeSpan interval)
{
Start((long)delay.TotalMilliseconds, (long)interval.TotalMilliseconds);
}
///
/// Stops the timer.
///
public void Stop()
{
m_running = false;
}
///
/// Updates the timer, firing the callback if enough time has elapsed.
///
/// the time this timer was called
public void Update(long systemTime)
{
if (!m_running)
return;
long timeDiff = systemTime - m_lastTick;
if (m_started == m_lastTick)
{
// we haven't run yet, check delay.
if (timeDiff >= m_delay)
{
// tick.
m_callback(this);
m_lastTick = systemTime;
}
}
else
{
if (timeDiff > 0 && timeDiff >= m_interval)
{
m_callback(this);
m_lastTick = systemTime;
}
}
}
///
/// Disposes the timer.
///
public void Dispose()
{
Stop();
m_callback = null;
}
}
}