Skip to content

Commit 6ca6d5a

Browse files
ArgoZhangwengy
andauthored
refactor(Timer): refactor prevent stuck thread (#5908)
* Revert "feat(Timer): remove Task.Run support wpf (#5896)" This reverts commit eadd595. * Reapply "feat(Timer): remove Task.Run support wpf (#5896)" This reverts commit d93de98. * refactor: 优化性能 * chore: bump version 9.5.11-beta06 Co-Authored-By: wengy <[email protected]> * test: 更新单元测试 * test: 增加单元测试 --------- Co-authored-by: wengy <[email protected]>
1 parent 2fde567 commit 6ca6d5a

File tree

3 files changed

+13
-29
lines changed

3 files changed

+13
-29
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.5.11-beta07</Version>
4+
<Version>9.5.11-beta06</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Timer/Timer.razor.cs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public partial class Timer
4343

4444
private CancellationTokenSource CancelTokenSource { get; set; } = new();
4545

46-
private AutoResetEvent ResetEvent { get; } = new(false);
47-
4846
private bool Vibrate { get; set; }
4947

5048
/// <summary>
@@ -185,29 +183,23 @@ private async Task OnStart(TimeSpan val)
185183
CancelTokenSource = new CancellationTokenSource();
186184
}
187185

188-
while (!CancelTokenSource.IsCancellationRequested && CurrentTimespan > TimeSpan.Zero)
186+
while (CancelTokenSource is { IsCancellationRequested: false } && CurrentTimespan > TimeSpan.Zero)
189187
{
190188
try
191189
{
192190
await Task.Delay(1000, CancelTokenSource.Token);
193-
}
194-
catch (TaskCanceledException) { }
195-
196-
if (!CancelTokenSource.IsCancellationRequested)
197-
{
198-
CurrentTimespan = CurrentTimespan.Subtract(TimeSpan.FromSeconds(1));
199-
StateHasChanged();
200-
}
201-
202-
if (IsPause)
203-
{
204-
ResetEvent.WaitOne();
205-
AlertTime = DateTime.Now.Add(CurrentTimespan).ToString("HH:mm:ss");
206191

207-
// 重建 CancelToken
208-
CancelTokenSource.Dispose();
209-
CancelTokenSource = new CancellationTokenSource();
192+
if (IsPause)
193+
{
194+
AlertTime = DateTime.Now.Add(CurrentTimespan).ToString("HH:mm:ss");
195+
}
196+
else
197+
{
198+
CurrentTimespan = CurrentTimespan.Subtract(TimeSpan.FromSeconds(1));
199+
StateHasChanged();
200+
}
210201
}
202+
catch (TaskCanceledException) { }
211203
}
212204

213205
if (CurrentTimespan == TimeSpan.Zero)
@@ -229,14 +221,6 @@ private async Task OnStart(TimeSpan val)
229221
private void OnClickPause()
230222
{
231223
IsPause = !IsPause;
232-
if (!IsPause)
233-
{
234-
ResetEvent.Set();
235-
}
236-
else
237-
{
238-
CancelTokenSource.Cancel();
239-
}
240224
}
241225

242226
private string GetPauseText() => IsPause ? ResumeText : PauseText;
@@ -262,7 +246,6 @@ protected override async ValueTask DisposeAsync(bool disposing)
262246
CancelTokenSource.Cancel();
263247
CancelTokenSource.Dispose();
264248

265-
ResetEvent.Dispose();
266249
if (Module != null)
267250
{
268251
await Module.DisposeAsync();

test/UnitTest/Components/TimerTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ public async Task OnCancel_Ok()
138138
Assert.True(buttons[1].ClassList.Contains("btn-warning"));
139139
Assert.Equal("暂停", buttons[1].GetInnerText());
140140
await cut.InvokeAsync(() => buttons[1].Click());
141+
await Task.Delay(1000);
141142

142143
// resume
143144
buttons = cut.FindAll(".timer-buttons button");

0 commit comments

Comments
 (0)