Skip to main content
.NET 8.0+

XPBaseCollection.LoadAsync() Method

Asynchronously loads persistent objects of a specific type from the data store into the collection.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v24.2.dll

Declaration

public virtual bool LoadAsync()

Returns

Type Description
Boolean

true if the current method call started collection loading; 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