namespace Common.Helpers; /// /// A that recurrs after a given interval. /// The callback is non-reentrant, unline a standard . /// public class RecurringTimer : IDisposable { private readonly System.Timers.Timer _timer; /// /// Instanciates the recurring timer, with the given. /// /// interval to act on callback, in milliseconds /// action to perform when the timer elapses public RecurringTimer(int intervalMs, Action callbackAction) { _timer = new System.Timers.Timer() { AutoReset = false, Interval = intervalMs }; _timer.Elapsed += delegate { callbackAction(); // perform callback action _timer.Start(); // manual restart after action finished }; } /// /// Starts the timing by setting to . /// public void Start() { _timer.Start(); } /// /// Stops the timing by setting to . /// public void Stop() { _timer.Stop(); } public void Dispose() { _timer?.Dispose(); } }