C#C
C#2y ago
Relevant

Long running process in Blazor Server

I have a Blazor Server app, and I have a page that will kick off a long running process. I am calling an async method without an await in order to get the response back quickly, and it is starting the processes fine. But it seems like whenever there is a long running syncronous task running in the background, it will lock the UI thread. How can I make this process run in a way that doesn't block the UI thread?

Here's a sample project to show what I mean:

public class TestService
{
    public void StartLongRunningJob()
    {
        LongRunningJob();
    }

    private async Task LongRunningJob()
    {
        while (true)
        {
            Thread.Sleep(5000); //Simulation of something that locks UI thread
            await Task.Delay(1);
        }
    }
}


@inject TestService TestService

<button class="btn btn-primary" @onclick="HandleButtonClick">Start Long Running Job</button>

@code{
    private void HandleButtonClick()
    {
        TestService.StartLongRunningJob();
    }
}
Was this page helpful?