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

Blazor 8 Timer Table update without button click

$
0
0

I have a Blazor 8 with InteractiveServerMode. I have a table that shows jobs to be done by a server. The jobs might be added every 20 sec (or an other program checks if there are any new jobs added and the adds them to the db if there are every 20 sec).I have tried to add a timer to the Blazor site to make it recheck the db for updates. But I can't get the table to update.Everything works without the timer (the table gets filled with the jobs that are in the db) but if I add a timer the table don't get filled.
html:

@if (jobsToDo.Count == 0){<p><em>Loading...</em></p>}else{<table class="table is-hoverable"><thead><tr><th>Id</th><th>Name</th><th>Priority</th><th>Est. Time</th><th>Owner</th></tr></thead><tbody>        @foreach (var job in jobsToDo)            {<tr><td>@job.Id</td><td>@job.Name</td><td class="columns m-0"><div class="column is-narrow p-0">@job.Priority</div><div class="column p-0">                            @if (job.Priority != 1)                            {<span class="icon" @onclick="() => UpdatePriorityUp(job)"><i class="fas fa-arrow-up"></i></span>                            }                            @if (job.Priority != jobsToDo.Count)                            {<span class="icon" @onclick="() => UpdatePriorityDown(job)"><i class=" fas fa-arrow-down"></i></span>                            }</div></td><td>@job.EstimetedTime</td><td>@job.Owner</td><td @onclick="() => DeleteJob(job)"><i class="fas fa-trash-alt"></i></td></tr>            }</tbody></table>}

C# code:

private List<JobToDo> jobsToDo = new List<JobToDo>();protected override async Task OnInitializedAsync()    {        Timer();    }private void Timer()    {        Timer timer = new Timer(20000);        timer.Enabled = true;        timer.Elapsed += async (sender, e) => await GetJobsToDo();        timer.Start();        timer.AutoReset = true;    }private async Task GetJobsToDo()    {        Console.WriteLine("in getjob");        using JobsContext db = DbFactory.CreateDbContext();        jobsToDo = await db.JobToDo.ToListAsync();        jobsToDo = jobsToDo.OrderBy(j => j.Priority).ToList();        await InvokeAsync(StateHasChanged);    }

The "in getjob" text is printed to the Console every 20 sec, so the timer is working but the table is not rendered at all.I have:

@rendermode InteractiveServer@attribute [StreamRendering(true)]

At the top of the file.If I add the GetJobsToDo() to OnInitializedAsync() the table is filled. @onclick events update the table as well.What are I missing?? Please help!


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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