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.
Lidarr/src/NzbDrone.Common/TPL/Debouncer.cs

72 lines
1.7 KiB

using System;
namespace NzbDrone.Common.TPL
{
public class Debouncer
{
protected readonly Action _action;
protected readonly System.Timers.Timer _timer;
protected readonly bool _executeRestartsTimer;
protected volatile int _paused;
protected volatile bool _triggered;
public Debouncer(Action action, TimeSpan debounceDuration, bool executeRestartsTimer = false)
{
_action = action;
_timer = new System.Timers.Timer(debounceDuration.TotalMilliseconds);
_timer.Elapsed += timer_Elapsed;
_executeRestartsTimer = executeRestartsTimer;
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (_paused == 0)
{
_triggered = false;
_timer.Stop();
_action();
}
}
public virtual void Execute()
{
lock (_timer)
{
_triggered = true;
if (_executeRestartsTimer)
{
_timer.Stop();
}
if (_paused == 0)
{
_timer.Start();
}
}
}
public virtual void Pause()
{
lock (_timer)
{
_paused++;
_timer.Stop();
}
}
public virtual void Resume()
{
lock (_timer)
{
_paused--;
if (_paused == 0 && _triggered)
{
_timer.Start();
}
}
}
}
}