-
-
Save sanalpencere/2c7774fa93e816a83fbfd3d47cb6904c to your computer and use it in GitHub Desktop.
ScheduledValue
This file contains 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
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