Skip to main content
A newer version of this page is available. .

JsonDataSource.FillAsync(CancellationToken) Method

Populates the JsonDataSource in an asynchronous manner and uses the cancellationToken parameter to send the cancellation signal.

Namespace: DevExpress.DataAccess.Json

Assembly: DevExpress.DataAccess.v20.2.dll

NuGet Packages: DevExpress.DataAccess, DevExpress.WindowsDesktop.DataAccess

Declaration

public Task FillAsync(
    CancellationToken cancellationToken
)

Parameters

Name Type Description
cancellationToken CancellationToken

A cancellation token that the task observes.

Returns

Type Description
Task

A task that populates the data source.

Remarks

Use this method to populate a JSON data source in an individual task asynchronously. Unlike the Fill() method call, FillAsync does not lock other actions performed concurrently. For instance, the user interface remains operational while the data source is populated.

Call the FillAsync method with the await operator.

The cancellationToken parameter provides a way to send the cancellation signal to the task. The task monitors the token and stops when it receives the signal. Create a CancellationTokenSource class instance and pass its Token property to the FillAsync method call. Call the CancellationTokenSource.Cancel method to stop the task.

Handle the FillError event, or catch the JsonDataSourceException exception, to process the case when the FillAsync method failed to populate the data source.

Example

The code sample below creates a JsonDataSource and populates it with data in an asynchronous manner. A CancellationTokenSource class instance is used to allow users to interrupt the data source fill process if it takes too long.

using System.Threading;
using System.Threading.Tasks;
using DevExpress.DataAccess;
using DevExpress.DataAccess.Json;
// ...
// Use the cancellationTokenSource to allow users to stop filling the data source.
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
// ...
// The cancelButton_Click event handler uses cancellationTokenSource to stop filling the data source.
private void cancelButton_Click(object sender, EventArgs e) {
    cancellationTokenSource.Cancel();
}
// ...
// Create a new JSON source.
var jsonSource = new UriJsonSource() {
    Uri = new Uri(@"https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json")
};
// Assign the JSON source to the data source.
var datasource = new JsonDataSource() {
    JsonSource = jsonSource
};
await datasource.FillAsync(cancellationTokenSource.Token);
See Also