I have simple Blazor Web Application (Server Side, .NET 8, VS2022).
Here main page:
@page "/"
@using System.Timers
@rendermode InteractiveServer
<p>@StatusMessage</p>
<button class="btn btn-primary" @onclick="StartHandler">Start</button>
@code {
private void StartHandler()
{
Countdown = 100;
timerCounter.Interval = 1000;
timerCounter.Elapsed += TimerCounter_Elapsed;
timerCounter.AutoReset = true;
timerCounter.Start();
}
private void TimerCounter_Elapsed(object? sender, ElapsedEventArgs e)
{
Countdown--;
StatusMessage = $"{Countdown}";
}
private Timer timerCounter = new Timer();
private int Countdown = 0;
private string StatusMessage = "";
}
It should start the timer on button click and show countdown on the page.
But when I click the button nothing happened.
On next clicks it shows count like 99 or 97.
I don’t understand what is going.
Why the timer event doesn’t fire?
Thanks.
I tried countdown on button’s click and it works.
But I need the timer fires event.