About StateHasChanged

I want to make a page that will refresh data once in a while.
I'm not sure if it's related to radzen. When the page is a little complicated (for example, the current user information is read and displayed), statehaschanged looks like crash, and then the timer stop.

there is timer code

protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            var timer = new Timer(new TimerCallback(_ =>
            {
                test++;

                this.InvokeAsync(() => this.StateHasChanged());
            }), null, 1000, 2000);



        }

    }

Is there a way to solve this problem?

The pasted code does not seem related to Radzen. Not sure what is causing the problem.

1 Like

I tried a lot of possibilities
It seems that in some cases statehaschanged will stop the timer
Using the button to trigger statehaschanged will work normally

It's strange. sad:(

now i know the reason.
I don't know what to say((
var timer = new Timer(new TimerCallback(_ =>
Timer has been GC by the system
if i declare timer in outsides, it will work normal
stupid pc;

And thank you very much for your reply!!