Tech Tip: Asynchronous Queries
Level 300
DevForce Professional
April 25, 2006
DevForce now supports asynchronous queries. An asynchronous query is a query that runs in the background on a separate thread while the user continues his/her current work without interruption. One version of asynchronous queries supports the ability to repeatedly execute the same query at a specified interval. This can be used to ensure that a client application is constantly up to date with the most current relevant data. Any number of asynchronous queries can be carried out simultaneously, limited solely by the availability of threads on the client machine.
Asynchronous queries are invoked in almost the same fashion as a regular query. In fact, any query can be executed asynchronously by simply calling one of the PersistenceManager.GetEntitiesAsync() overloads.
C#:
// Get the Default PersistenceManager.
PersistenceManager pm = PersistenceManager.DefaultManager;
// This is the query for all orders placed within the last 7 days.
RdbQuery query1 = new RdbQuery(typeof(Order),
Order.OrderDateEntityColumn, EntityQueryOp.GT,
DateTime.Today - new TimeSpan(7, 0, 0, 0));
// Register an event handler to be called when the query completes.
pm.GetEntitiesCompleted += new EventHandler(pm_GetEntitiesCompleted);
// Start the executing query.
// when it completes, the pm_GetEntitiesCompleted
// referenced above will be called.
// The token "anyValue" will be returned in the
// GetEntitiesCompletedEventArgs to distinguish
// this async call from any others.
pm.GetEntitiesAsync(q, QueryStrategy.DataSourceOnly, "anyValue");
VB.NET:
' Get the Default PersistenceManager.
Dim pm As PersistenceManager = PersistenceManager.DefaultManager
' This is the query for all orders placed within the last 7 days.
Dim q As RdbQuery = New RdbQuery (GetType (Order), _
Order.OrderDateEntityColumn, EntityQueryOp.GT, _
DateTime.Today - New TimeSpan(7, 0, 0, 0))
' Register an event handler to be called when the query completes.
AddHandler pm.GetEntitiesCompleted, AddressOf _
New EventHandler(Of GetEntitiesCompletedEventArgs) _
(pm_GetEntitiesCompleted)
' Start the executing query.
' When it completes, the pm_GetEntitiesCompleted referenced above
' will be called.
' The token "anyValue" will be returned in the
' GetEntitiesCompletedEventArgs to distinguish
' this async call from any others.
pm.GetEntitiesAsync(q, QueryStrategy.DataSourceOnly, _
"anyValue")