Skip to main content
.NET Framework 4.5.2+

XPBaseCollection.LoadAsync(AsyncLoadObjectsCallback) Method

Asynchronously loads persistent objects of a specific type from the data store into the collection, and notifies upon completion.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v24.1.dll

NuGet Package: DevExpress.Xpo

Declaration

public virtual bool LoadAsync(
    AsyncLoadObjectsCallback callback
)

Parameters

Name Type Description
callback DevExpress.Xpo.Helpers.AsyncLoadObjectsCallback

A DevExpress.Xpo.Helpers.AsyncLoadObjectsCallback delegate to be called after loading is complete.

Use callback to do the following:

  • Iterate a collection of loaded persistent objects passed as a parameter, or store it for later use within the original thread.
  • Store the exception information passed as a parameter, and use this information later to raise the exception again, within the original thread.

Do not raise exceptions or modify persistent objects within a callback.

Returns

Type Description
Boolean

true if the current method call started the collection load; otherwise, false.

Remarks

To reload data from a data store to the collection, call the XPBaseCollection.Reload method.

We recommend using the LoadAsync(CancellationToken) Task-based overload instead of this method.

Note

If the XPBaseCollection.LoadingEnabled property is set to false, calling the LoadAsync method has no effect.

Example

The following example demonstrates how to asynchronously work with persistent objects.

In this example, a collection (xpcPersons) of Person objects is used as a data source for a grid control (gridPersons). The Update and Commit buttons (btnUpdate and btnCommit) allow users to update the grid control and store changes to a data store while continuing to interact with other UI elements. The accessibility of the grid control and these buttons is automatically updated within operation callbacks (CallBackLoadPersons and CallBackCommitChanges).

using DevExpress.Xpo;
using DevExpress.Xpo.DB;

namespace AsyncOperations {

    // A custom XPObject
    class Person : XPObject {
       //...
    }

    public class AsyncLoading {
        UnitOfWork unitOfWork;
        // Initialize unitOfWork here.
        //...

        // An XPCollection to be used as a data source for the gridPersons grid control
        XPCollection xpcPersons;

        //...

        // The Click event handler of the Update button
        private void btnUpdate_Click(object sender, EventArgs e) {
            if (xpcPersons == null) {
                xpcPersons = new XPCollection(unitOfWork, typeof(Person));
                LoadAsyncPersons();
                gridPersons.DataSource = xpcPersons;
            } else {
                LoadAsyncPersons();
            }
        }

        // Disables buttons and the grid control and initiates asynchronous collection loading
        void LoadAsyncPersons() {
            btnUpdate.Enabled = false;
            btnCommit.Enabled = false;
            gridPersons.Enabled = false;
            xpcPersons.LoadAsync(new DevExpress.Xpo.Helpers.AsyncLoadObjectsCallback(
                CallBackLoadPersons));
        }
        // Called after collection loading is complete
        // Re-enables buttons and the grid control
        void CallBackLoadPersons(ICollection[] result, Exception ex) {
            btnUpdate.Enabled = true;
            btnCommit.Enabled = true;
            gridPersons.Enabled = true;
            if (ex != null) {
                MessageBox.Show(ex.Message);
            }
        }

        // The Click event handler of the Commit button
        // Disables buttons and the grid control and asynchronously commits changes
        private void btnCommit_Click(object sender, EventArgs e) {
            btnUpdate.Enabled = false;
            btnCommit.Enabled = false;
            gridPersons.Enabled = false;
            unitOfWork.CommitChangesAsync(new AsyncCommitCallback(CallBackCommitChanges));
        }

        // Called after changes have been saved
        // Re-enables buttons and the grid control
        void CallBackCommitChanges(Exception ex) {
            btnUpdate.Enabled = true;
            btnCommit.Enabled = true;
            gridPersons.Enabled = true;
            if (ex != null) {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
See Also