aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Core
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2018-12-31 18:01:25 +0200
committerRoy Ben-Shabat <Roy@Twine-s.com>2018-12-31 18:01:25 +0200
commit3415150ae12893d4f08d1afde512624755996095 (patch)
treef40cfea549bd10ee28476e7d4e1dfd6f1d9c064c /Software/Visual_Studio/Tango.Core
parent413d16064a1cf2975c13aad728d6ed87294bf35e (diff)
downloadTango-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')
-rw-r--r--Software/Visual_Studio/Tango.Core/Tango.Core.csproj1
-rw-r--r--Software/Visual_Studio/Tango.Core/Threading/ActionTimer.cs47
2 files changed, 48 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Core/Tango.Core.csproj b/Software/Visual_Studio/Tango.Core/Tango.Core.csproj
index 5bfe3ab79..99d3c00f5 100644
--- a/Software/Visual_Studio/Tango.Core/Tango.Core.csproj
+++ b/Software/Visual_Studio/Tango.Core/Tango.Core.csproj
@@ -89,6 +89,7 @@
<Compile Include="Json\IJsonSerializationController.cs" />
<Compile Include="Json\ProtobufContractResolver.cs" />
<Compile Include="Json\SerializationControllerContractResolver.cs" />
+ <Compile Include="Threading\ActionTimer.cs" />
<Compile Include="Threading\TaskSequencer.cs" />
<Compile Include="Threading\ThreadFactory.cs" />
<Compile Include="Threading\TimeoutTask.cs" />
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();
+ }
+ }
+}