Canceling a long-running task when users navigate away
Depending on the traffic in your application, long-running tasks can negatively impact performance and user experience if you don’t properly manage them. With components rendered in SSR mode, the server handles cancellations for you, similar to what happens in web API projects. But in interactive modes, when state is persisted either on the server or the client side when users navigate away from a page where a long-running task is in progress, it’s essential that you gracefully cancel the task to free up resources and prevent unnecessary processing.
Let’s implement a graceful cancellation of long-running tasks with the help of Blazor’s NavigationManager
and CancellationToken
.
Getting ready
Before we explore graceful cancellation of a long-running task when a user navigates away, do the following:
- Create a
Chapter09
/Recipe07
directory – this will be your working directory - Copy...