I've posted an answer to a question in stackoverflow C# counter to count up to a target number. Here's the answer:
You can create a timer service that can serve you on many occasions:
Create the service class:
public class BlazorTimer
{
private Timer _timer;
internal void SetTimer(double interval)
{
_timer = new Timer(interval);
_timer.Elapsed += NotifyTimerElapsed;
_timer.Enabled = true;
_timer.Start();
}
private void NotifyTimerElapsed(object sender, ElapsedEventArgs e)
{
OnElapsed?.Invoke();
}
public event Action OnElapsed;
}
Add the service to the DI container, in the Program.Main method, as transient:
builder.Services.AddTransient(config =>
{
var blazorTimer = new BlazorTimer();
blazorTimer.SetTimer(1000);
return blazorTimer;
});
Usage
@page "/"
@implements IDisposable
@inject BlazorTimer Timer
@count.ToString()
@code{
private int count = 0;
protected override void OnInitialized()
{
Timer.OnElapsed += NotifyTimerElapsed;
base.OnInitialized();
}
private void NotifyTimerElapsed()
{
// Note: WebAssembly Apps are currently supporting a single thread, which
// is why you don't have to call
// the StateHasChanged method from within the InvokeAsync method. But it
// is a good practice to do so in consideration of future changes, such as
// ability to run WebAssembly Apps in more than one thread.
InvokeAsync(() => { count++; StateHasChanged(); });
}
public void Dispose()
{
Timer.OnElapsed -= NotifyTimerElapsed;
}
}
However, I'm told that
the BlazorTimer is leaking the _timer. Timer is IDisposable
Does unsubscribing the event handler in the Dispose method implemented in a Blazor component causes the BlazorTimer leaking the _timer. Actually I do not entirely understand "the BlazorTimer is leaking the _timer. Timer is IDisposable" , so let me ask, how can I prevent the leaking of the timer, and yet use code to unsubscribe the event handler in the Dispose method implemented in a Blazor component ? Is there any way to prevent this leaking other than skipping the un-subscription of the event handler.