This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.cs
66 lines (54 loc) · 1.53 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
using System;
using System.Threading;
namespace Tomato
{
public delegate void TimerTick(object sender, EventArgs e);
public class Timer
{
const int SLEEP_TIME = 1000;
private Thread innerThread;
public Boolean Enabled;
public TimerTick Tick;
public Timer()
{
innerThread = new Thread(DoEvents);
innerThread.Priority = ThreadPriority.Normal;
innerThread.Start();
}
private void DoEvents()
{
try
{
while (true)
{
var start = DateTime.Now.Ticks;
if (Enabled)
{
if (Tick != null)
{
Tick.Invoke(null, null);
}
}
var end = DateTime.Now.Ticks;
var spent = new TimeSpan(end - start).Ticks;
var sleepTime = (int)(SLEEP_TIME - spent / TimeSpan.TicksPerMillisecond);
if (sleepTime <= 0)
{
continue;
}
Thread.Sleep(sleepTime);
}
}
catch (ThreadAbortException) { }
}
public void Abort()
{
this.Enabled = false;
if (innerThread != null)
{
innerThread.Abort();
innerThread = null;
}
}
}
}