Skip to content

Instantly share code, notes, and snippets.

@sanalpencere
Created May 14, 2020 07:57
Show Gist options
  • Save sanalpencere/2c7774fa93e816a83fbfd3d47cb6904c to your computer and use it in GitHub Desktop.
Save sanalpencere/2c7774fa93e816a83fbfd3d47cb6904c to your computer and use it in GitHub Desktop.
ScheduledValue
using System;
using System.Timers;
namespace Vagon
{
public class ScheduledValue<T>
{
private readonly T _value;
private readonly Timer _timer;
private readonly Action<T> _action;
/// <summary>
///
/// </summary>
/// <param name="action">Object</param>
/// <param name="value">Object</param>
/// <param name="interval">In Milliseconds</param>
public ScheduledValue(Action<T> action, T value, int interval = 1000)
{
_value = value;
_action = action;
_timer = new Timer(interval);
_timer.Elapsed += Timer_Elapsed;
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
_timer.Stop();
_action?.Invoke(_value);
}
public void Schedule()
{
_timer.Start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment