Multiprocotol Terminalprogram (BAT)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MultiTerm/Common/Helpers/RecurringTimer.cs

51 lines
1.4 KiB

namespace Common.Helpers;
/// <summary>
/// A <see cref="System.Timers.Timer"/> that recurrs after a given interval.
/// The callback is non-reentrant, unline a standard <see cref="System.Timers.Timer"/>.
/// </summary>
public class RecurringTimer : IDisposable
{
private readonly System.Timers.Timer _timer;
/// <summary>
/// Instanciates the recurring timer, with the <paramref name="intervalMs"/> given.
/// </summary>
/// <param name="intervalMs">interval to act on callback, in milliseconds</param>
/// <param name="callbackAction">action to perform when the timer elapses</param>
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
};
}
/// <summary>
/// Starts the timing by setting <see cref='System.Timers.Timer.Enabled'/> to <see langword='true'/>.
/// </summary>
public void Start()
{
_timer.Start();
}
/// <summary>
/// Stops the timing by setting <see cref='System.Timers.Timer.Enabled'/> to <see langword='false'/>.
/// </summary>
public void Stop()
{
_timer.Stop();
}
public void Dispose()
{
_timer?.Dispose();
}
}