Live Chat Icon For mobile
Live Chat Icon

How do I dispose a page in Blazor WebAssembly?

Platform: Blazor| Category : WebAssembly, General

The Dispose method is used to avoid memory leaks and allows proper garbage collection. Implement the IDisposable interface in the component and call the Dispose method. If the framework calls the Dispose method, the component is removed from the UI and unmanaged resources can be released.

The following example demonstrates the disposal of Timer in the counter page.


@page "/counter"
@using System.Timers
@implements IDisposable

<h1>Counter with Timer disposal</h1>

<p>Current count: @currentCount</p>

@code {
    private int currentCount = 0;
    private Timer timerObj = new(1000);

    protected override void OnInitialized()
        timerObj.Elapsed += (sender, eventArgs) => OnTimerCallback();

    private void OnTimerCallback()
        InvokeAsync(() =>

    public void Dispose()

View Sample in GitHub

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.