-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimedEvent.cs
More file actions
85 lines (66 loc) · 1.82 KB
/
TimedEvent.cs
File metadata and controls
85 lines (66 loc) · 1.82 KB
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
namespace Pluton.Core
{
using System;
using System.Collections.Generic;
using System.Timers;
public class TimedEvent : CountedInstance
{
Dictionary<string, object> _args;
readonly string _name;
readonly Timer _timer;
long lastTick;
ulong _elapsedCount = 0;
ulong elapsedReachedUInt64Max = 0;
public delegate void TimedEventFireDelegate(TimedEvent evt);
public event TimedEventFireDelegate OnFire;
public TimedEvent(string name, double interval)
{
_name = name;
_timer = new Timer();
_timer.Interval = interval;
_timer.Elapsed += _timer_Elapsed;
_elapsedCount = 0;
}
public TimedEvent(string name, double interval, Dictionary<string, object> args)
: this(name, interval)
{
_args = args;
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (OnFire != null)
OnFire(this);
if (_elapsedCount == UInt64.MaxValue) {
_elapsedCount = 0;
elapsedReachedUInt64Max++;
if (elapsedReachedUInt64Max == UInt64.MaxValue)
elapsedReachedUInt64Max = 0;
}
_elapsedCount++;
lastTick = DateTime.UtcNow.Ticks;
}
public void Start()
{
_timer.Start();
lastTick = DateTime.UtcNow.Ticks;
}
public void Stop() => _timer.Stop();
public void Kill()
{
_timer.Stop();
_timer.Dispose();
}
public Dictionary<string, object> Args {
get { return _args; }
set { _args = value; }
}
public double Interval {
get { return _timer.Interval; }
set { _timer.Interval = value; }
}
public string Name => _name;
public double TimeLeft => (Interval - ((DateTime.UtcNow.Ticks - lastTick) / 0x2710L));
public ulong ElapsedCount => _elapsedCount;
public string Elapsed => elapsedReachedUInt64Max == 0 ? _elapsedCount.ToString() : elapsedReachedUInt64Max.ToString() + "x" + UInt64.MaxValue.ToString() + " + " + _elapsedCount;
}
}