We have a .Net Core 6 app, which we are slowly transitioning UI functionality from JS to Blazor, using Blazor Server components. For the most part everything works fine, however we introduced a couple components that have timers for refreshing status of items that can change outside of the user actions, things like network status. Everything works fine, except that when moving away from the page that the component is on, the component’s Dispose method is never called, which leaves the timer running.
Here is how the component is called from the cshtml page:
<component type="typeof(APStatus)" render-mode="Server" />
Here is the timer definition in the OnInitializedAsync.
_timer = new System.Timers.Timer(10000);
_timer.Elapsed += CountDownTimer;
_timer.Start();
_timer.Enabled = true;
Here is the CountDownTimer callback:
public async void CountDownTimer(Object source, System.Timers.ElapsedEventArgs e)
{
if (_internalCounter > 0)
{
_internalCounter -= 1;
}
else
{
_timer.Enabled = false;
_timer.Stop();
}
await InvokeAsync(async () =>
{
if (NetInfo != null)
{
await GetApStatus();
}
});
}
Here is Dispose Method:
public void Dispose()
{
System.Diagnostics.Debug.WriteLine("Disposing Happened");
_timer.Enabled = false;
_timer.Stop();
_timer.Dispose();
_timer = null;
}
I added a CircuitHandler, same problem, I get OnCircuitOpenAsync events, but the Closed Circuit never fires.
Here is some of the CircuitHandler code:
public override Task OnCircuitClosedAsync(Circuit circuit, CancellationToken cancellationToken)
{
Console.WriteLine("OnCircuitClosedAsync");
Circuits.TryRemove(circuit.Id, out var circuitRemoved);
OnCircuitsChanged();
return base.OnCircuitClosedAsync(circuit, cancellationToken);
}
I have tried virtually everything I can think of, gone through the Program.cs, various ways of calling the component, everything seems to be working fine from the chrome dev tools standpoint, no strange error messages in the logs.
I turned on blazor logging, and that hasn’t shown anything.
I have searched virtually every forum I can find for someone with a similar problem, but can’t find anything on why this would be occurring.
I have other blazor components on site, everything is working fine. I’ve added Dispose to all of those, and none of them ever get fired.
I’ve updated to the latest .net core 6 packages, no luck.
I started a fresh project, and implemented the Microsoft code for a timer in a blazor component, it works fine, the Dispose is called as expected when navigating away from the page.
When I copied that component into our project, everything works fine, but when I navigate away from the page with the component on, nothing, Dispose is never called.
Here is the counter Example:
@page "/counter"
@using System.Timers
@using System.Diagnostics
@implements IDisposable
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<p>Current count: @timerCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
private int timerCount = 0;
private Timer timer = new(1000);
protected override void OnInitialized()
{
timer.Elapsed += (sender, eventArgs) => OnTimerCallback();
timer.Start();
}
private void OnTimerCallback()
{
_ = InvokeAsync(() =>
{
timerCount++;
StateHasChanged();
});
}
public void Dispose()
{
Debug.WriteLine("Disposing");
timer.Dispose();
}
}
Jim Miller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.