Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

What is the best way to use PeriodicTimer for refresh component in blazor server

$
0
0

I have a component that needs to display fresh data every 1 minute.

I tried the new timer (Periodic Timer) and it works fine.

Now, my questions are,

Where is the best place to put the while loop?

Is something else required for a proper dispose of the PeriodicTimer?

public partial class Index:IDisposable{    private readonly PeriodicTimer _periodicTimer = new(TimeSpan.FromMinutes(1));    protected override async Task OnInitializedAsync()    {        await Service.Init();        while (await _periodicTimer.WaitForNextTickAsync())        {            await Service.GetViewModels();            await InvokeAsync(StateHasChanged);        }    }    public void Dispose()    {        Dispose(true);        GC.SuppressFinalize(this);    }    protected virtual void Dispose(bool disposing)    {        if (disposing)        {           _periodicTimer.Dispose();        }    }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>