Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the recommended way to release the UI from a long running background task in Blazor client-side

Tags:

azure

blazor

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)

like image 691
Laurence73 Avatar asked Apr 01 '19 12:04

Laurence73


People also ask

Should I use Blazor WebAssembly VS server?

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.

What is client-side Blazor?

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.

Is Blazor server-side rendered?

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.

What is _framework Blazor server JS?

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 .


2 Answers

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 ...");
                }
            });
    }
}
like image 138
dani herrera Avatar answered Oct 17 '22 04:10

dani herrera


If async calls does not help then you can use browser workers, just need to implement some js interop.

like image 40
Vakhtangi Abashidze Avatar answered Oct 17 '22 04:10

Vakhtangi Abashidze