In an application, you don’t want to waste resources. When an ongoing operation is not needed anymore, you should cancel it. For instance, if your application downloads data to show them in a view, and the user navigates away from this view, you should cancel the download.

In a .NET application, the way to cancel operation is to use a CancellationToken. You can create a CancellationToken using the CancellationTokenSource. Once the Blazor component is removed from the page, you can call the Cancel method to stop the ongoing operations.

According to the Blazor component lifecycle, you can call the Cancel method in the IDisposable.Dispose() method.

#blazor #.net #asp.net core #web

Canceling Background Tasks When A User Navigates Away From A Blazor Component
3.70 GEEK