Our Blazor (client-side) app is made up of many components all existing on the UI at the same time. One of these has to do a number of large data calls to Azure SQL. This component does these calls regardless of whether it has UI focus or not. Each of calls these can take up to 3 seconds to return its result during which it renders the UI unresponsive. How can we keep the UI responsive during these calls without using Blazor server-side. Using Task.Run etc does not help in single threaded architecture. Using loading spinners is also not an option as this still leaves the UI unresponsive and may not be visible to the user. Is there any way to achieve this goal in current Blazor 0.9.0?
Running latest Blazor preview release (0.9.0-preview3-19154-02)
Blazor WebAssembly apps can run offline, which is particularly useful when clients aren't able to connect to the Internet. Blazor Server apps fail to run when the connection to the server is lost. If an app must run offline, Blazor WebAssembly is the best choice.
Client-side Blazor makes use of WebAssembly, which is used to run high-level languages on browsers. Necessary . NET WebAssembly-related code and its dependencies are downloaded to the browser, and the app is executed directly on the browser UI thread. All UI updates and event handling occur within the same process.
What is Server-Side Blazor? Since Blazor is a client-side web framework, therefore, the component logic and DOM interaction, both happen in the same process. However, the design of the Blazor framework is smart and flexible enough to run the application separate from the rendering process.
Blazor is an open-source, single-page web application development framework. Unlike other frameworks, such as Angular, React and Vue, which depend on JavaScript libraries, Blazor allows you to write and run C# code in web browsers via WebAssembly. The Blazor framework can, however, call JavaScript functions from .
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 ...");
}
});
}
}
If async calls does not help then you can use browser workers, just need to implement some js interop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With