-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathLoopThread.cs
75 lines (64 loc) · 1.81 KB
/
LoopThread.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
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System;
using System.Threading;
namespace Telegram.Common
{
public partial class LoopThread
{
private TimeSpan _interval;
private readonly object _timerLock = new();
private readonly Timer _timer;
public TimeSpan Interval => _interval;
public LoopThread(TimeSpan interval)
{
_interval = interval;
_timer = new Timer(OnTick, null, Timeout.Infinite, Timeout.Infinite);
}
[ThreadStatic]
private static LoopThread _chats;
public static LoopThread Chats => _chats ??= new LoopThread(TimeSpan.FromMilliseconds(1000 / 60));
private void OnTick(object state)
{
lock (_timerLock)
{
_tick?.Invoke(this, EventArgs.Empty);
}
}
public void Rent()
{
_count++;
}
public void Release()
{
_count--;
}
private int _count;
public int Count => _count;
public bool HasMoreResources => _count < 120;
private event EventHandler _tick;
public event EventHandler Tick
{
add
{
lock (_timerLock)
{
if (_tick == null) _timer.Change(TimeSpan.Zero, _interval);
_tick += value;
}
}
remove
{
lock (_timerLock)
{
_tick -= value;
if (_tick == null) _timer.Change(Timeout.Infinite, Timeout.Infinite);
}
}
}
}
}