Page reload

I have been reading every example for setting up a timer, they are all on setting a timer for a grid or some other component. I really need to set up a timer to execute the load event on a page ever 60 seconds. I looked at the code example you gave for grid.Reload. I can't add code because the that part is autogenerated and won't keep the change. Can you tell me how to implement a timer for page.load?

1 Like

Usually Radzen generates Load() method for every page with the code added in the property grid, you can execute this method.

Yes, I utilize the load event quite heavily for several pages, but I am trying to figure out how to implement a timer in the load event that executes the load event every 60 seconds. Do I execute C#, invoke a method or something else. If it was a straight blazor page, I would implement the code there, but the page is auto generated and I don't want to put it in the ignore list. Any examples I have found here have partial code and don't really show how to use Radzen to implement, just straight code. If I am asking something that is outside of what support would answer, that's fine, just tell me. If anyone has an idea of where I can find these answers, that would be very helpful.

1 Like

You cannot add partial code in Radzen and what you are looking for is only possible using partial code. You can use your code editor of choice to add the custom code.

Here is what I have done for one of my projects where I needed to refresh data or reload the page. I have a page called "MyDashboard.razor". In my MyDashbaord.razor.cs file I created a Timer that gets called from the Load event in MyDashboard.razor.designer.cs. Hopefully this gives you an idea of what you could do.

    Timer timer;
    private bool StartTimer()
    {
        if(timer == null)
        {
            
            timer = new Timer(60000);
            timer.Elapsed += Timer_Elapsed;
            timer.Start();
            return true;
        }
        else
        {
            timer.Stop();
            timer.Start();
            return true;
        }
        
    }

    private async void Timer_Elapsed(object sender, ElapsedEventArgs e)
    {           
        
        await InvokeAsync(() =>
        {
            Load();
        });


    }
1 Like

OK! Thank you for this, it works awesome. Took me a little while to figure out that I just needed to add it to the partial class xyzcomponent and then call it as an invoke method. Now that I have messed around with it, it also solves many other problems I have. I am new to C# and Blazor, and this is great! Thanks again!

Again, thank you for the help with this, just one question....I have notices that the time continues to run even when I leave that page. Is there a way to dispose it when you leave that page?

This is what I did. I used UriHelper.LocationChanged event to stop the timer

 private void InitTimer()
        {
            base.OnInitialized();

              timer = new Timer(60000);
            timer.Elapsed += (s, e) => { InvokeAsync(Timer_Elapsed); };
            timer.Start();
            UriHelper.LocationChanged += (s, e) => { InvokeAsync(StopTimer); };
        
        }

private void StopTimer()
        {
            timer.Stop();
        }
2 Likes

Awesome, thank you for the help!