-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimeOutQueue.cs
159 lines (157 loc) · 4.81 KB
/
TimeOutQueue.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
151
152
153
154
155
156
157
158
159
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
namespace TEArts.Etc.CollectionLibrary
{
public class TimeOutQueue<TEArtsType> : IDisposable
{
private SortedList<DateTime, List<TimedItem<TEArtsType>>> List = new SortedList<DateTime, List<TimedItem<TEArtsType>>>();
private Timer Timer;
//public EventHandler<TEArtsType> OnTimeOut;
public long Millisecond { get; private set; }
public DateTime Next { get; private set; }
public TimeOutQueue() : base()
{
Timer = new Timer(x =>
{
DateTime dt = ((DateTime)(x));
if (!List.ContainsKey(dt))
{
return;
}
List<TimedItem<TEArtsType>> t = List[dt];
foreach (TimedItem<TEArtsType> tt in t)
{
if (tt.NextTimer < 0)
{
tt.Callback(tt.Item);
}
else
{
tt.NextTimer -= long.MaxValue;
Add(tt);
}
}
t.Clear();
List.Remove(dt);
nextTimer();
}, Next, Timeout.Infinite, Timeout.Infinite);
Next = DateTime.MaxValue;
}
public TimeOutQueue(long mill) : this()
{
Millisecond = mill;
}
public TimedItem<TEArtsType> Add(TEArtsType item, Action<TEArtsType> onTimeOut)
{
return Add(item, Millisecond, onTimeOut);
}
public TimedItem<TEArtsType> Add(TEArtsType item, long mill, Action<TEArtsType> onTimeOut)
{
TimedItem<TEArtsType> i = new TimedItem<TEArtsType>(Millisecond);
return Add(i);
}
public TimedItem<TEArtsType> Add(TimedItem<TEArtsType> i)
{
if (i.TimeOut <= DateTime.Now)
{
i.ResetTime();
}
long m = 0;
TimeSpan t = (i.TimeOut - DateTime.Now);
if (t.TotalMilliseconds > long.MaxValue)
{
m = long.MaxValue;
i.NextTimer = t.TotalMilliseconds - long.MaxValue;
}
else
{
m = (long)(t.TotalMilliseconds);
}
List<TimedItem<TEArtsType>> ts = null;
lock (List)
{
if (List.ContainsKey(i.TimeOut))
{
ts = List[i.TimeOut];
}
else
{
ts = new List<TimedItem<TEArtsType>>();
}
ts.Add(i);
if (Next > i.TimeOut)
{
Timer.Change(m, Timeout.Infinite);
}
}
return i;
}
public void Remove(TimedItem<TEArtsType> item)
{
lock (List)
{
if (List.ContainsKey(item.TimeOut))
{
List[item.TimeOut].Remove(item);
}
if (List[item.TimeOut].Count == 0)
{
nextTimer();
}
}
}
private void nextTimer()
{
if (List.Keys.Count > 0)
{
Next = List.Keys.Min();
Timer.Change(((long)((Next - DateTime.Now).TotalMilliseconds)), Timeout.Infinite);
}
else
{
Timer.Change(Timeout.Infinite, Timeout.Infinite);
}
}
public void Clear()
{
List.Clear();
nextTimer();
}
public void Dispose()
{
Timer.Change(Timeout.Infinite, Timeout.Infinite);
Timer.Dispose();
List.Clear();
}
}
public class TimedItem<TEArtsType>
{
public TimedItem(double millisecond)
{
if (millisecond < 0) millisecond = 50;
TimeOut = DateTime.Now.AddMilliseconds(millisecond);
NextTimer = -1;
}
public TimedItem(TEArtsType value, DateTime timeout, Action<TEArtsType> action)
{
Item = value;
Callback = action;
if (timeout < DateTime.Now)
{
timeout = DateTime.Now.AddMilliseconds(50);
}
TimeOut = timeout;
NextTimer = -1;
}
internal void ResetTime()
{
TimeOut = DateTime.Now.AddMilliseconds(50);
}
internal double NextTimer { get; set; }
public TEArtsType Item { get; private set; }
public DateTime TimeOut { get; private set; }
public Action<TEArtsType> Callback { get; private set; }
}
}