Helper Class
public class IntervalTimer{
DateTime _lastCheckpoint;
TimeSpan _tripInterval;
public IntervalTimer(TimeSpan tripInterval)
{
_lastCheckpoint = DateTime.UtcNow;
_tripInterval = tripInterval;
}
public bool IsTripped
{
get
{
return DateTime.UtcNow >
_lastCheckpoint.Add(_tripInterval);
}
}
public void Reset()
{
_lastCheckpoint = DateTime.UtcNow;
}
}
Usage
// initialize a timer that trips every minutevar intervalTimer = new IntervalTimer(TimeSpan.FromMinutes(1));
// this code should probably be run on a separate thread
// especially for a GUI application
while(true)
{
if (intervalTimer.IsTripped)
{
// Do your thing here (log, publish metrics, etc.)
intervalTimer.Reset();
}
Thread.Sleep(1000);
}