What is the recommended way to release the UI from a long running background task in Blazor client-side
You can use Invoke
, I modified counter
example page to illustrate it, you will need a kind of singleton DI object to avoid running the process for twice.
Remember Blazor
is an experimental project. Also, this answer is also an experimental approach.
@page "/counter"
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" onclick="@IncrementCount">Click me</button>
@functions {
int currentCount = 0;
void IncrementCount()
{
currentCount++;
}
protected override void OnInit()
{
Invoke(
//here your task.
async () =>
{
for(var i =0; i< 50; i++)
{
await Task.Delay(1000);
currentCount++;
StateHasChanged();
System.Console.WriteLine("Still running ...");
}
});
}
}