2022-01-22 21:54:41 +01:00

46 lines
1018 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-success" @onclick="Start">Start</button>
<button class="btn btn-danger" @onclick="Stop">Stop</button>
<button class="btn btn-secondary" @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();
}
}