This repository has been archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Timer.cs
150 lines (123 loc) · 3.89 KB
/
Timer.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
namespace EnhancedMap
{
public static class TimerManager
{
private static readonly List<Timer> _Timers = new List<Timer>();
private static readonly ConcurrentQueue<Timer> _ToAdd = new ConcurrentQueue<Timer>();
private static readonly ConcurrentQueue<Timer> _ToRemove = new ConcurrentQueue<Timer>();
private static readonly AutoResetEvent _waitHandle = new AutoResetEvent(true);
static TimerManager()
{
Thread t = new Thread(Run) {IsBackground = true};
t.Start();
}
private static void Run()
{
while (MainCore.IsRunning)
{
_waitHandle.WaitOne(1);
if (_ToAdd.Count > 0 || _ToRemove.Count > 0)
{
while (_ToAdd.Count > 0)
{
if (_ToAdd.TryDequeue(out Timer t))
_Timers.Add(t);
}
while (_ToRemove.Count > 0)
{
if (_ToRemove.TryDequeue(out Timer t))
_Timers.Remove(t);
}
}
foreach (var t in _Timers)
{
if (!t.IsRunning) continue;
if (t._delay > 0 && t._firstRun)
{
if (DateTime.Now > t._nextDelay)
t._firstRun = false;
else
continue;
}
if (DateTime.Now > t._next)
{
t.OnTick();
t._next = DateTime.Now.AddMilliseconds(t._interval);
}
}
}
}
public static Timer Create(int interval, Action callback, bool started = true)
{
return Create(interval, 0, callback, started);
}
public static Timer Create(int interval, int delay, Action callback, bool started = true)
{
Timer t = Timer.CallBack(interval, delay, callback);
Add(t);
if (started)
t.Start();
return t;
}
private static void Add(Timer t)
{
_ToAdd.Enqueue(t);
}
public static void Remove(Timer t)
{
if (t.IsRunning)
t.Stop();
_ToRemove.Enqueue(t);
}
}
public abstract class Timer
{
internal bool _firstRun;
internal int _interval, _delay;
internal DateTime _next, _nextDelay;
protected Timer(int interval)
{
_interval = interval;
_next = DateTime.Now;
}
protected Timer(int interval, int delay) : this(interval)
{
_delay = delay;
}
public bool IsRunning { get; private set; }
public abstract void OnTick();
public void Start()
{
IsRunning = true;
if (_delay > 0)
{
_nextDelay = DateTime.Now.AddMilliseconds(_delay);
_firstRun = true;
}
}
public void Stop()
{
IsRunning = false;
}
internal static Timer CallBack(int interval, int delay, Action callback)
{
return new TimerState(interval, delay, callback);
}
private class TimerState : Timer
{
private readonly Action _callback;
public TimerState(int interval, int delay, Action callback) : base(interval, delay)
{
_callback = callback;
}
public override void OnTick()
{
_callback();
}
}
}
}