Why loading indicator doesn't show by default in Blazor Server application?
I've created a Blazor Server app using standard template with WeatherForecastService in it.
I've added Thread.Sleep(2000) at the beginning of GetForecastAsync method to simulate long time operation.
Result is that I don't see FetchData page until 2 seconds tick away.
On the other hand if I put await Task.Run in FetchData.razor like below:
protected override async Task OnInitializedAsync()
{
base.OnInitializedAsync();
//forecasts = await ForecastService.GetForecastAsync(DateTime.Now); //not working as should?
await Task.Run(LoadData); //works fine
}
async Task LoadData(){
forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
}
Then I see the "Loading...".
Is something not right with the default FetchData or am I doing something wrong?
The UI has to run on 1 Thread. When you Sleep() on that thread then there is no UI for the duration.
With Task.Run() you move LoadData() to another Thread so the show can go on. But that only works for Blazor Server. On WebAssembly there are no 'other' threads.
Sleep() can be used to simulate synchronous (CPU intensive) work.
Task.Delay() simulates asynchronous I/O.