TestLiveUpdate/Pages/Counter.razor
2022-01-22 12:24:55 +01:00

46 lines
1017 B
Plaintext

@page "/counter"
@using System.Diagnostics
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p role="status">Current count: @_currentCount</p>
<p role="status">Runtime: @_stopwatch.Elapsed.ToString("g")</p>
<button class="btn btn-primary" @onclick="Start">Start</button>
<button class="btn btn-primary" @onclick="Stop">Stop</button>
<button class="btn btn-primary" @onclick="Reset">Reset</button>
@code {
private int _currentCount = 0;
private bool _cancel;
private readonly Stopwatch _stopwatch = new();
private async Task Start()
{
_stopwatch.Start();
_cancel = false;
while (!_cancel)
{
_currentCount++;
StateHasChanged();
await Task.Delay(10);
}
_stopwatch.Stop();
}
private void Stop()
{
_cancel = true;
}
private void Reset()
{
_cancel = true;
_currentCount = 0;
_stopwatch.Reset();
}
}