New to Telerik UI for ASP.NET Core? Download free 30-day trial

Use CancellationToken in ToDataSourceResultAsync to Stop Query

Environment

Product Progress® Kendo UI® Grid for ASP.NET MVC

Description

How can I cancel the asynchronous call to database query or some other action if the time limit is reached or due to another problem?

Solution

The ToDatasourceResultAsync method can be used to achieve asynchronous programming as demonstrated in this sample:

Add Asynchronous Calls to Action Methods With ToDataSourceResultAsync

In real-case scenarios, developers usually have also to consider other factors like Connection Time Limit, Invalid Input, etc. To help with this task, we exposed the CancellationToken parameter which comes handy in this type of scenarios:

        public async Task<ActionResult> EditingInline_Read([DataSourceRequest] DataSourceRequest request)
        {
            CancellationTokenSource source = new CancellationTokenSource(2000);
            CancellationToken token = source.Token;

            var products = await productService.Read().ToDataSourceResultAsync(request, token);

            return Json(products);
        }

This feature is available both for MVC and Core Telerik UI toolsets. A full runnable sample for ASP.NET Core can be found here:

Asynchronous Binding with Cancellation Token

See Also

In this article
Not finding the help you need? Improve this article