diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-12-31 18:01:25 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-12-31 18:01:25 +0200 |
| commit | 3415150ae12893d4f08d1afde512624755996095 (patch) | |
| tree | f40cfea549bd10ee28476e7d4e1dfd6f1d9c064c /Software/Visual_Studio/Tango.Core/Threading | |
| parent | 413d16064a1cf2975c13aad728d6ed87294bf35e (diff) | |
| download | Tango-3415150ae12893d4f08d1afde512624755996095.tar.gz Tango-3415150ae12893d4f08d1afde512624755996095.zip | |
Implemented ActionTimer.
Fixed some issue with machine designer and dispenser module.
Diffstat (limited to 'Software/Visual_Studio/Tango.Core/Threading')
| -rw-r--r-- | Software/Visual_Studio/Tango.Core/Threading/ActionTimer.cs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Core/Threading/ActionTimer.cs b/Software/Visual_Studio/Tango.Core/Threading/ActionTimer.cs new file mode 100644 index 000000000..48ee12964 --- /dev/null +++ b/Software/Visual_Studio/Tango.Core/Threading/ActionTimer.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Timers; + +namespace Tango.Core.Threading +{ + /// <summary> + /// Represents an action executer with a predefined interval and a reset mechanism. + /// </summary> + public class ActionTimer + { + private Timer _timer; + private Action _action; + + /// <summary> + /// Initializes a new instance of the <see cref="ActionTimer"/> class. + /// </summary> + /// <param name="interval">The interval.</param> + public ActionTimer(TimeSpan interval) + { + _timer = new Timer(interval.TotalMilliseconds); + _timer.Enabled = true; + _timer.Stop(); + _timer.Elapsed += _timer_Elapsed; + } + + /// <summary> + /// Resets the current timer and replaces the action to be invoked. + /// </summary> + /// <param name="action">The action.</param> + public void ResetReplace(Action action) + { + _timer.Stop(); + _action = action; + _timer.Start(); + } + + private void _timer_Elapsed(object sender, ElapsedEventArgs e) + { + _timer.Stop(); + _action?.Invoke(); + } + } +} |
