© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
1 reply
! ziptie.entity

StateHasChanged not working in timers.

Hello, am trying to make a countdown timer to a specific date, it all works but for some reason even though I am calling
StateHasChanged
StateHasChanged
, it doesn't update, it only updates when I refresh the page.
I also noticed that the dispose method (in the IDisposable interface) is called immediately after the page refreshes and the timer gets disposed instantly so it never has any time to run.

.NET Version: 8.0.302

@implements IDisposable

<span>@days, @hours, @minutes, @seconds</span>

@code {
    private int days;
    private int hours;
    private int minutes;
    private int seconds;

    private Timer? timer;

    protected override void OnInitialized()
    {
        StartTimer();
    }

    private void StartTimer()
    {
        DateTime targetDate = new DateTime(2024, 12, 31, 23, 59, 59);
        UpdateCountdown(targetDate);

        timer = new Timer(state =>
        {
            UpdateCountdown(targetDate);
            InvokeAsync(StateHasChanged);
        }, null, 0, 1000);
    }

    private void UpdateCountdown(DateTime targetDate)
    {
        var timeSpan = targetDate - DateTime.Now;

        if (timeSpan.TotalSeconds < 0)
        {
            days = 0;
            hours = 0;
            minutes = 0;
            seconds = 0;
            timer?.Dispose();
        }
        else
        {
            days = timeSpan.Days;
            hours = timeSpan.Hours;
            minutes = timeSpan.Minutes;
            seconds = timeSpan.Seconds;
        }
    }

    public void Dispose()
    {
        timer?.Dispose();
    }
}
@implements IDisposable

<span>@days, @hours, @minutes, @seconds</span>

@code {
    private int days;
    private int hours;
    private int minutes;
    private int seconds;

    private Timer? timer;

    protected override void OnInitialized()
    {
        StartTimer();
    }

    private void StartTimer()
    {
        DateTime targetDate = new DateTime(2024, 12, 31, 23, 59, 59);
        UpdateCountdown(targetDate);

        timer = new Timer(state =>
        {
            UpdateCountdown(targetDate);
            InvokeAsync(StateHasChanged);
        }, null, 0, 1000);
    }

    private void UpdateCountdown(DateTime targetDate)
    {
        var timeSpan = targetDate - DateTime.Now;

        if (timeSpan.TotalSeconds < 0)
        {
            days = 0;
            hours = 0;
            minutes = 0;
            seconds = 0;
            timer?.Dispose();
        }
        else
        {
            days = timeSpan.Days;
            hours = timeSpan.Hours;
            minutes = timeSpan.Minutes;
            seconds = timeSpan.Seconds;
        }
    }

    public void Dispose()
    {
        timer?.Dispose();
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Blazor StateHasChanged not working properly?
C#CC# / help
2y ago
❔ Timers are confusing
C#CC# / help
3y ago
Invoke StateHasChanged() only after asynchronous operation has completed
C#CC# / help
2y ago
Testing a class that relies on timers
C#CC# / help
4y ago