-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandler.cs
More file actions
130 lines (116 loc) · 3.48 KB
/
Handler.cs
File metadata and controls
130 lines (116 loc) · 3.48 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
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
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace MicroTimer
{
public class Handler : INotifyPropertyChanged
{
private static Handler? _handler;
private static CosTimer? _timer = null;
private static int _period = 1; // 计时周期
private static bool _pause = true;
private static bool _stop = true;
private static bool _swapText = false;
private static int _freshRate { get; set; } = 200;
private static int _refreshTicks = 0;
private static int _refreshInterval = 0;
public event PropertyChangedEventHandler? PropertyChanged;
public static bool Stop => _stop;
public static TimeOnly TimeNow { get; private set; } = new(0, 0, 0, 0);
public string TimeNowString => _swapText ? TimeNow.ToString("fff") : TimeNow.ToString("HH:mm:ss");
public string MsNowString => _swapText ? TimeNow.ToString("HH:mm:ss") : TimeNow.ToString("fff");
public static Handler Instance
{
get
{
if (_handler == null)
{
_handler = new Handler();
}
return _handler;
}
}
private Handler()
{
SetRefreshRate(_freshRate);
InitializeTimer();
}
private void InitializeTimer()
{
_timer = new CosTimer(_period, TimerMode.PERIODIC);
_timer.OnRunning += _timer_OnRunning;
_timer.Start();
}
private void _timer_OnRunning(ulong ticks)
{
if (!_pause && !_stop)
{
UpdateTime();
}
}
private void UpdateTime()
{
TimeNow = TimeNow.Add(new TimeSpan(0, 0, 0, 0, _period));
if (_refreshTicks >= _refreshInterval)
{
_refreshTicks = 0;
ChangeUI();
}
else
{
_refreshTicks++;
}
}
private void ChangeUI()
{
OnPropertyChanged(nameof(TimeNow));
OnPropertyChanged(nameof(TimeNowString));
OnPropertyChanged(nameof(MsNowString));
}
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void Dispose()
{
if (_timer != null)
{
_timer.Dispose();
}
_handler = null;
}
public void SetRefreshRate(int rate)
{
if (rate <= 0) return;
_freshRate = rate;
_refreshInterval = (int)(1000.0 / _freshRate / _period - 1);
}
public void SwapText()
{
_swapText = !_swapText;
OnPropertyChanged(nameof(TimeNowString));
OnPropertyChanged(nameof(MsNowString));
}
public void SingleAction()
{
if (_pause)
{
_pause = false;
_stop = false;
}
else
{
_pause = true;
}
UpdateTime();
ChangeUI();
}
public void ResetAction()
{
// 重置并暂停
_pause = true;
_stop = true;
TimeNow = new(0, 0, 0, 0);
ChangeUI();
}
}
}