-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSTimer.cs
43 lines (36 loc) · 934 Bytes
/
STimer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using Object = System.Object;
using System.Timers;
using System;
using Timer = System.Timers.Timer;
namespace RedotUtils;
/// <summary>
/// If for whatever reason a Timer is needed on a non-Godot thread, this is what you should use.
/// </summary>
public class STimer : IDisposable
{
private readonly Timer _timer;
public STimer(double delayMs, Action action, bool enabled = true, bool autoreset = true)
{
void Callback(Object source, ElapsedEventArgs e) => action();
_timer = new Timer(delayMs);
_timer.Enabled = enabled;
_timer.AutoReset = autoreset;
_timer.Elapsed += Callback;
}
public void Start()
{
_timer.Enabled = true;
}
public void Stop()
{
_timer.Enabled = false;
}
public void SetDelay(double delayMs)
{
_timer.Interval = delayMs;
}
public void Dispose()
{
_timer.Dispose();
}
}