SqlDataSource.FillAsync(CancellationToken) Method
Populates the SqlDataSource in an asynchronous manner and uses the cancellationToken parameter to send the cancellation signal.
Namespace: DevExpress.DataAccess.Sql
Assembly: DevExpress.DataAccess.v24.2.dll
Declaration
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 an SQL 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.
All queries are validated one by one. If any validation errors occur, an AggregateException is thrown with these errors. Use the exception’s InnerExceptions property to access a collection of QueryExecutionException objects (use their InnerException property to access an actual ValidationException).
If the validation succeeds, all queries are executed one by one. If any errors occur, an AggregateException is thrown with these errors. Use the exception’s InnerExceptions property to access a collection of QueryExecutionException objects (use their InnerException property to access an actual DevExpress.DataAccess.Native.Sql.SqlExecutionException).
To execute only specific queries, use a FillAsync method overload that accepts the queriesToFill parameter.
Example
The code sample below creates an SqlDataSource 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 DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
// ...
SqlDataSource DataSource { get; set; }
// ...
public void CreateSqlDataSource()
{
MsSqlConnectionParameters connectionParameters = new MsSqlConnectionParameters(
".", "NorthWind", null, null, MsSqlAuthorizationType.Windows);
var sqlDataSource = new SqlDataSource(connectionParameters) { Name = "Sql_Categories" };
var categoriesQuery = SelectQueryFluentBuilder
.AddTable("Categories")
.SelectAllColumnsFromTable()
.Build("Categories");
categoriesQuery.Name = "queryCategoriesAllFields";
sqlDataSource.Queries.Add(categoriesQuery);
sqlDataSource.RebuildResultSchema();
DataSource = sqlDataSource;
}
// ...
// Use the cancellationTokenSource to allow users to stop the task.
System.Threading.CancellationTokenSource cancellationTokenSource =
new System.Threading.CancellationTokenSource();
// ...
// A user can click the button to stop the task.
private void cancelButton_Click(object sender, System.EventArgs e)
{
cancellationTokenSource.Cancel();
}
// ...
// ...
// Click the button to create and fill the data source.
private async void queryButton_Click(object sender, EventArgs e) {
try
{
CreateSqlDataSource();
await FillDataSourceAsyncAllowCancel();
}
catch (TaskCanceledException ex)
{
MessageBox.Show("Task canceled");
}
}
// ...
async Task FillDataSourceAsyncAllowCancel()
{
await DataSource.FillAsync(cancellationTokenSource.Token);
}