Using a timer to update a page

I have a timer that implements the Load event of a calendar page every 10 minutes (600000 miliseconds). I am trying to use IDisposable to stop the timer when the calendar is no longer displayed in the UI (navigate to another page... programmatically or when selecting another page from the sidebar). It looks like this can only be added to the .razor page. My actual question is...Since this page is autogenerated do i have to put it in the ignore list to assure that it does not get over written? Or is there another way to implement IDisposable?

Update:

I read the other questions on timers and IDisposable.

Following is my timer code:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Radzen;
using Radzen.Blazor;
using System.Timers;

namespace SmgAnalytix2.Pages
{
public partial class DealCalendarComponent
{
Timer timer;
private bool StartTimer()
{
if (timer == null)
{

            timer = new Timer(600000);
            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();
        });


    }
}

}

When I try to implement IDisposable on this, I get an error that says Dispose() is already being used in the designer.cs file. Which I assume is this piece of code...

namespace SmgAnalytix2.Pages
{
public partial class DealCalendarComponent : ComponentBase, IDisposable
{
[Parameter(CaptureUnmatchedValues = true)]
public IReadOnlyDictionary<string, dynamic> Attributes { get; set; }

    [Inject]
    protected GlobalsService Globals { get; set; }

    **public void Dispose()**
    {
        Globals.PropertyChanged -= OnPropertyChanged;
    }

    public void Reload()
    {
        InvokeAsync(StateHasChanged);
    }

    public void OnPropertyChanged(PropertyChangedEventArgs args)
    {
    }

Not sure how to get the dispose to work...any ideas?

Maybe we can add partial method that will be called during dispose and you can use it in your partial class?

That might do it. It looks like that Dispose is used for Globals? on all pages...

Yep, it's generated when there are global properties only.

Ok, so as it is now, I can't dispose a timer if I am using any global values. I there a way around this?

We will release update Monday with the partial OnDispose method.

Thank You!!!! Much appreciated!

Trying to use the new OnDispose partial method to stop the timer. Really running into a roadblock here. An possibility of pointing in the right direction for implementing this? I have the timer code in my razor.cs file but can't seem to figure out where and how to use in the OnDispose. Sorry for my ignorance on this. I have looked everywhere before checking here...nothing gives me the information I am looking for.

Thanks for the help!

Hey @daveg1466,

As we promised in our latest update there is OnDispose() method that can be used in the page partial class:


Trying to figure out how to use it. Can't seem to.

This is what you've wrote in your second reply in this thread. Now you can access Dispose() method via partial OnDispose().

Have a look at this as it may help