Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 35 additions & 41 deletions src/BootstrapBlazor/Components/Timer/Timer.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,68 +168,62 @@ private async Task Timeout()
}
}

private Task OnStart(TimeSpan val)
private async Task OnStart(TimeSpan val)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Synchronous ResetEvent.WaitOne call inside an async method.

Using ResetEvent.WaitOne is a blocking operation and may block the async state machine if executed on a UI or a limited thread pool thread. Consider leveraging an asynchronous wait mechanism to avoid potential deadlocks or UI freezes.

Suggested implementation:

                if (IsPause)
                {
                    await Task.Run(() => ResetEvent.WaitOne());
                    AlertTime = DateTime.Now.Add(CurrentTimespan).ToString("HH:mm:ss");
                }

Make sure the surrounding method is declared as async and that any callers are updated accordingly. This change leverages an asynchronous wait for the ResetEvent to avoid blocking the async flow.

Value = val;
IsPause = false;
CurrentTimespan = Value;
AlertTime = DateTime.Now.Add(CurrentTimespan).ToString("HH:mm:ss");

StateHasChanged();
Copy link

Copilot AI Apr 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] If using await Task.Yield() to force an async context switch, please consider adding a brief comment explaining its purpose to aid future maintainers.

Suggested change
StateHasChanged();
StateHasChanged();
// Force an asynchronous context switch to ensure UI updates are processed before continuing.

Copilot uses AI. Check for mistakes.
await Task.Yield();

_ = Task.Run(async () =>
// 点击 Cancel 后重新设置再点击 Star
if (CancelTokenSource.IsCancellationRequested)
{
// 点击 Cancel 后重新设置再点击 Star
if (CancelTokenSource.IsCancellationRequested)
CancelTokenSource.Dispose();
CancelTokenSource = new CancellationTokenSource();
}

while (!CancelTokenSource.IsCancellationRequested && CurrentTimespan > TimeSpan.Zero)
{
try
{
CancelTokenSource.Dispose();
CancelTokenSource = new CancellationTokenSource();
await Task.Delay(1000, CancelTokenSource.Token);
}
catch (TaskCanceledException) { }

while (!CancelTokenSource.IsCancellationRequested && CurrentTimespan > TimeSpan.Zero)
if (!CancelTokenSource.IsCancellationRequested)
{
try
{
await Task.Delay(1000, CancelTokenSource.Token);
}
catch (TaskCanceledException) { }

if (!CancelTokenSource.IsCancellationRequested)
{
CurrentTimespan = CurrentTimespan.Subtract(TimeSpan.FromSeconds(1));
await InvokeAsync(StateHasChanged);
}
CurrentTimespan = CurrentTimespan.Subtract(TimeSpan.FromSeconds(1));
StateHasChanged();
}

if (IsPause)
{
ResetEvent.WaitOne();
AlertTime = DateTime.Now.Add(CurrentTimespan).ToString("HH:mm:ss");
if (IsPause)
{
ResetEvent.WaitOne();
AlertTime = DateTime.Now.Add(CurrentTimespan).ToString("HH:mm:ss");

// 重建 CancelToken
CancelTokenSource.Dispose();
CancelTokenSource = new CancellationTokenSource();
}
// 重建 CancelToken
CancelTokenSource.Dispose();
CancelTokenSource = new CancellationTokenSource();
}
}

if (CurrentTimespan == TimeSpan.Zero)
if (CurrentTimespan == TimeSpan.Zero)
{
await Task.Delay(500, CancelTokenSource.Token);
if (!CancelTokenSource.IsCancellationRequested)
{
await Task.Delay(500, CancelTokenSource.Token);
if (!CancelTokenSource.IsCancellationRequested)
Value = TimeSpan.Zero;
Vibrate = IsVibrate;
StateHasChanged();
if (OnTimeout != null)
{
Value = TimeSpan.Zero;
await InvokeAsync(async () =>
{
Vibrate = IsVibrate;
StateHasChanged();
if (OnTimeout != null)
{
await OnTimeout();
}
});
await OnTimeout();
}
}
});
return Task.CompletedTask;
}
}

private void OnClickPause()
Expand Down
Loading