Last active
December 17, 2015 23:29
-
-
Save adamsteinert/5689260 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// A simple class to encapsulate the functionality for a restartable timer. When Restart is called the timer is stopped | |
/// so the time interval starts again from that point. | |
/// </summary> | |
public class DelayTimer | |
{ | |
DispatcherTimer _timer; | |
Action _tickAction; | |
#region -- Events --- | |
public event EventHandler Tick; | |
void _timer_Tick(object sender, EventArgs e) | |
{ | |
_timer.Stop(); | |
if (Tick != null) | |
Tick(sender, e); | |
if (_tickAction != null) | |
_tickAction(); | |
} | |
#endregion | |
#region -- Construction -- | |
public DelayTimer(int milliseconds) | |
{ | |
_timer = new DispatcherTimer() | |
{ | |
Interval = new TimeSpan(0, 0, 0, 0, milliseconds), | |
IsEnabled = false | |
}; | |
_timer.Tick += new EventHandler(_timer_Tick); | |
} | |
#endregion | |
/// <summary> | |
/// Restart the timer. | |
/// </summary> | |
/// <param name="complete">Optional action to perform when the delay timer fires. Tick event still fires</param> | |
public void Restart(Action completeAction = null) | |
{ | |
_tickAction = completeAction; | |
_timer.Stop(); | |
_timer.Start(); | |
} | |
/// <summary> | |
/// Stops the timer and clears the tick action. | |
/// </summary> | |
public void Reset() | |
{ | |
//_tickAction = null; | |
_timer.Stop(); | |
} | |
public bool IsEnabled | |
{ | |
get | |
{ | |
return _timer != null && _timer.IsEnabled; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment