Using CancellationToken in ToDataSourceResultAsync to Stop Query
Environment
Product | Progress® Kendo UI® Grid for UI for ASP.NET Core |
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-life 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
For more information on asynchronous programming, see Microsoft's docs.