|
3 | 3 | using System.ComponentModel; |
4 | 4 | using System.Net.Http; |
5 | 5 | using System.Threading.Tasks; |
| 6 | +using System.Timers; |
6 | 7 | using Avalonia; |
7 | 8 | using Avalonia.Controls; |
8 | 9 | using Avalonia.Data; |
@@ -48,7 +49,14 @@ public RpcView() |
48 | 49 | tblText1.DataContext = _tblText1; |
49 | 50 | tblText2.DataContext = _tblText2; |
50 | 51 | ViewType = ViewType.Default; |
51 | | - _rpcClient.Disconnected += (sender, args) => _timerTime = null; |
| 52 | + _timer = new Timer(1000); |
| 53 | + _timer.Elapsed += TimerOnElapsed; |
| 54 | + _rpcClient.Disconnected += (sender, args) => |
| 55 | + { |
| 56 | + _timer.Stop(); |
| 57 | + _timerTime = null; |
| 58 | + tblTime.Text = string.Empty; |
| 59 | + }; |
52 | 60 | } |
53 | 61 |
|
54 | 62 | private static readonly Dictionary<Uri, IBrush> CachedImages = new Dictionary<Uri, IBrush>(); |
@@ -111,10 +119,19 @@ private async void PresenceOnPropertyChanged(object? sender, PropertyChangedEven |
111 | 119 | } |
112 | 120 |
|
113 | 121 | private DateTime? _timerTime; |
| 122 | + private readonly Timer _timer; |
114 | 123 | private void RpcClientOnPresenceUpdated(object? sender, PresenceMessage e) => this.RunUILogic(() => |
115 | 124 | { |
116 | 125 | _timerTime = e.Presence.Timestamps?.Start; |
117 | | - _ = RunTimer(); |
| 126 | + if (_timerTime != null) |
| 127 | + { |
| 128 | + _timer.Start(); |
| 129 | + } |
| 130 | + else |
| 131 | + { |
| 132 | + _timer.Stop(); |
| 133 | + tblTime.Text = string.Empty; |
| 134 | + } |
118 | 135 |
|
119 | 136 | tblTitle.Text = e.Name; |
120 | 137 | tblText1.Text = e.Presence.Details; |
@@ -201,27 +218,13 @@ private async Task UpdateSmallImage(Uri? uri) |
201 | 218 | } |
202 | 219 | } |
203 | 220 |
|
204 | | - private bool _timerRunning; |
205 | | - private async Task RunTimer() |
| 221 | + private void TimerOnElapsed(object? sender, ElapsedEventArgs e) |
206 | 222 | { |
207 | | - if (_timerRunning) |
208 | | - { |
209 | | - return; |
210 | | - } |
211 | | - _timerRunning = true; |
212 | | - |
213 | | - while (_timerTime.HasValue) |
214 | | - { |
215 | | - var ts = DateTime.UtcNow.Subtract(_timerTime.Value); |
216 | | - |
217 | | - var text = ts.Hours > 0 ? ts.Hours.ToString("00") + ":" : string.Empty; |
218 | | - text += $"{ts.Minutes:00}:{ts.Seconds:00}"; |
219 | | - tblTime.Text = text; |
220 | | - await Task.Delay(1000); |
221 | | - } |
| 223 | + var ts = DateTime.UtcNow.Subtract(_timerTime.Value); |
222 | 224 |
|
223 | | - _timerRunning = false; |
224 | | - tblText1.Text = string.Empty; |
| 225 | + var text = ts.Hours > 0 ? ts.Hours.ToString("00") + ":" : string.Empty; |
| 226 | + text += $"{ts.Minutes:00}:{ts.Seconds:00}"; |
| 227 | + this.RunUILogic(() => tblTime.Text = text); |
225 | 228 | } |
226 | 229 |
|
227 | 230 | private async Task<bool> ProcessUri(Uri uri) |
|
0 commit comments