Asynchronous Mode
- 7 minutes to read
PivotGridControl can perform data-aware operations (retrieving data from the server, sorting, grouping, filtering data and calculating summaries) in a background thread, allowing the entire application to stay responsive while these operations are in progress.
#Overview
If PivotGridControl is working with a large dataset, retrieving data from a data source, as well as performing various data-aware operations such as sorting or filtering data, may take a considerable amount of time. In a standard (synchronous) mode, the entire application would not respond to user actions until PivotGridControl completes the required operations, since the primary UI thread is busy. End-users can notice a small or considerable application freeze.
In an asynchronous mode, only PivotGridControl is frozen. The application continues interacting with an end-user, while the control is processing data in a background thread. While processing data, PivotGridControl displays a loading panel to indicate that this control is busy and would not respond to user actions until the operation is completed.
#UI Operations
You can specify whether data-aware operations caused by end-user actions run synchronously or asynchronously. To do this, use the PivotGridControl.UseAsyncMode property. If this property is set to true, all operations performed via a UI are asynchronous.
By default, the PivotGridControl.UseAsyncMode property is set to true, and all end-user actions trigger asynchronous operations.
#Asynchronous Methods
In code, you can perform both synchronous and asynchronous operations, regardless of the UI settings. All methods whose names end with ...Async are asynchronous, i.e., they start executing the related operation in a background thread and return control.
Each asynchronous method has an overload that allows you to specify custom actions to be performed after the asynchronous operation is completed. To do this, pass an AsyncCompletedHandler delegate that performs the required actions as the asyncCompleted parameter. If an asynchronous method returns data (an operation result), you must pass a delegate to obtain this data. The delegate's result parameter returns an AsyncOperationResult instance that exposes the following properties.
Gets the result of the asynchronous operation. | |
Gets an exception thrown during the asynchronous operation execution. |
#Asynchronous Control Updates
You can perform a data-aware operation asynchronously, even if there is no special ...Async method implemented. PivotGridControl provides the PivotGridControl.EndUpdateAsync method that allows you to apply changes in a background thread. For instance, you can customize the pivot grid via its properties, wrap the code in the PivotGridControl.BeginUpdate and PivotGridControl.EndUpdateAsync method calls, and the control will be updated asynchronously.
To ensure that the PivotGridControl.EndUpdateAsync method is executed even if an exception occurs, wrap calls to the PivotGridControl.BeginUpdate and PivotGridControl.EndUpdateAsync methods in a try..finally statement.
#Execution of Asynchronous Operations
The PivotGridControl performs the following actions after an asynchronous operation has been triggered:
- closes all popup menus and windows, disables the UI and loses focus (if it was focused);
- raises the PivotGridControl.AsyncOperationStarting event;
- locks access to pivot grid data through API, shows a loading panel and runs background calculations.
After background calculations are finished, the PivotGridControl does the following:
- unlocks access to pivot grid data through API, hides the loading panel and activates the UI;
- raises the PivotGridControl.AsyncOperationCompleted event;
- performs internal handling of the operation results;
- executes a custom delegate (if one has been passed).
#OLAP Mode Specifics
An OLAP mode introduces several specific aspects that affect the usage of synchronous and asynchronous operations.
Restriction on Synchronous Operations.
Since the WebClient for Silverlight does not support synchronous query processing, synchronous operations cannot be performed in an OLAP mode. Therefore, the pivot grid throws an exception in the following instances.
- An end-user has performed an action that requires access to the data source, with the PivotGridControl.UseAsyncMode property set to false.
- A method that triggers a synchronous data-aware operation has been called in code.
Data Binding
In an OLAP mode, data binding operations are performed asynchronously.
If you specify a data source in the markup, an asynchronous data binding operation will be started during pivot grid initialization. While this operation is in progress, it is unsafe to access the pivot grid. The PivotGridControl.OlapDataLoaded event notifies you when data binding is completed.
If you specify a data source in code, use the PivotGridControl.SetDataSourceAsync method to assign a PivotGridXmlaDataSource object to the pivot grid. You can pass a delegate that will notify you when the data binding operation is completed as the method's parameter.
Data-Aware Operations Caused by Property Value Changing
Specific properties trigger a data-aware operation when they are modified. These are properties that specify an area where a particular field is located (PivotGridField.Area), as well as the field index in this area (PivotGridField.AreaIndex), and field values sort order (PivotGridField.SortOrder), etc.
In an OLAP mode, data-aware operations caused by property modifications are performed asynchronously.
If you subsequently change two or more properties, PivotGridControl will be updated only once, after the last action. In other words, you may think that in an OLAP mode, property operations are always wrapped in the PivotGridControl.BeginUpdate and PivotGridControl.EndUpdateAsync method calls, so that you do not need to call these methods explicitly.
NOTE
This does not refer to the Pivot
Grid property.Control. Data Source
#Event Handling Specifics
In an asynchronous mode, events that allow you to control summary calculation are handled in a background thread. This regards the following events.
- PivotGridControl.CustomUnboundFieldData
- PivotGridControl.CustomFieldSort
- PivotGridControl.CustomGroupInterval
- PivotGridControl.CustomSummary
- PivotGridControl.FieldValueDisplayText (handled in a background thread if a field's PivotGridField.SortMode property is set to FieldSortMode.DisplayText)
For thread safety, you cannot use event parameter's properties that return PivotGridField objects in an asynchronous mode, since it is not safe to access pivot grid fields from a background thread. Each of these properties has a thread-safe counterpart, whose name starts with the prefix ThreadSafe. These properties return objects that implement the IThreadSafeField interface, which provides thread-safe read-only access to pivot grid fields, allowing you to obtain the required information.
#Event Notification
PivotGridControl raises the PivotGridControl.AsyncOperationStarting and PivotGridControl.AsyncOperationCompleted events before an asynchronous operation is started and after it has been completed, respectively.
#Member Tables
This section lists methods that are exposed by PivotGridControl and that perform various operations asynchronously.
Data Binding
Reloads data from the control data source and recalculates summaries asynchronously. | |
Creates Pivot | |
Sets a data source for Pivot |
Sorting
Toggles the sort order of the specified field asynchronously. | |
Sets the specified sort order for the specified field asynchronously. |
Layout Operations
Expands or collapses all values of the specified field asynchronously. | |
Collapses all columns and rows in Pivot | |
Collapses all columns asynchronously. | |
Collapses all rows asynchronously. | |
Collapses the specified column or row asynchronously. | |
Expands all columns and rows in the Pivot | |
Expands all columns asynchronously. | |
Expands all rows asynchronously. | |
Expands the specified column or row asynchronously. |
Serialization
Method | Description |
---|---|
Pivot |
Restores the collapsed state of field values from the specified stream asynchronously. |
Pivot |
Restores the pivot grid layout from the specified stream asynchronously. Allows you to specify custom actions to be performed after this operation is completed. |
Pivot |
Restores the pivot grid's layout from the specified XML file asynchronously. Allows you to specify custom actions to be performed after this operation is completed. |
Drill-Down Operations
Returns a list of records used to calculate summary values for all cells asynchronously. | |
In OLAP mode, returns a list of records used to calculate a summary value for the specified cell asynchronously. Allows you to specify which columns should be returned. |
Control Update
Unlocks the Pivot |