Target is to start a long running background task entering the Razor page. Changes done in the database by the background task are updated in the table of the Razor page.
I can change the menue on the left and can go back to the page. But when I hit F5 during page is running, I get the following error: System.ObjectDisposedException: 'Cannot process pending renders after the renderer has been disposed. Object name: 'Renderer'.'
I don't know if the approach taken is completely wrong and if it must be done in another way.
I expected starting a fire and forget task in the Razor page doesn't harm the page (razor page):
protected override async Task OnInitializedAsync()
{
ViewModel.PropertyChanged += async (sender, e) =>
{
await InvokeAsync(() =>
{
StateHasChanged();
});
};
await base.OnInitializedAsync();
if (!ViewModel.IsBusy)
{
_ = Task.Factory.StartNew(ViewModel.AddItems);
}
}
Method in the ViewModel:
public void AddItems()
{
IsBusy = true;
CancellationTokenSource stoppingToken = null;
stoppingToken = new CancellationTokenSource();
CancellationToken token = stoppingToken.Token;
for (int i = 0; i < 10; i++)
{
List.Add($"{i}");
OnPropertyChanged(nameof(List));
var cancellationTriggered = token.WaitHandle.WaitOne(1000);
}
IsBusy = false;
}
An example to reproduce the problem you can find here: enter link description here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.