Invoking Business Tasks Asynchronously

The Service Adapter supports asynchronous InvokeTask calls to the AppServer. Business Task methods may be called asynchronously with a similar signature than the synchronous InvokeTask calls.


The ServiceAdapter provides InvokeTaskAsync methods for zero to five ProDatasets. These methods are defined in the Interface IServiceAdapterWithInvokeTaskAsync.

A sample call to InvokeTaskAsync looks like this:
CAST (FrameworkSettings:ServiceAdapter, IServiceAdapterWithInvokeTaskAsync):InvokeTaskAsync

            ("async",                          /* AppServer Partition */
             "Test.SCL246.TestTask",           /* Task Class Name */
             "TaskMethod",                     /* Task Method Name */
             DATASET dsCustomer:HANDLE,        /* Handle of the first parameter dataset */
             DATASET dsCountry:HANDLE,         /* Handle of the second parameter dataset */
             oParameter,                       /* ISerializable parameter object */
             ?,                                /* Context dataset handle, ? for default context dataset */
             sender) .                         /* Context object reference */

The last parameter (sender in the above sample) is a reference to an object that may help to distinguish between multiple async AppServer calls when the AsyncTaskCompleted event is raised. This may either be the reference to a visual object (a Form) or a value object (e.g. a CharacterHolder holding a GUID) or any other object reference.

When the async call is finished, the Service Adapter raises the AsyncTaskCompleted event

CAST (FrameworkSettings:ServiceAdapter, IServiceAdapterWithInvokeTaskAsync):AsyncTaskCompleted:Subscribe (AsyncTaskCompletedHandler) 

 

Note, that you should unsubscribe from the event (e.g. in an OnFormClosed override):

/*------------------------------------------------------------------------------
    Purpose: Raises the Form closed event 
    Notes:   
    @param e The FormClosedEventArgs with the data for this event 
------------------------------------------------------------------------------*/
METHOD PROTECTED OVERRIDE VOID OnFormClosed (e AS System.Windows.Forms.FormClosedEventArgs):

    CAST (FrameworkSettings:ServiceAdapter, IServiceAdapterWithInvokeTaskAsync):AsyncTaskCompleted:Unsubscribe (AsyncTaskCompletedHandler) .

    SUPER:OnFormClosed (e) . 
 
END METHOD .

 

When the AsyncTaskCompleted event is raised, the ProDatasets with the handles provided in the InvokeTaskAsync method call are already populated with the data returned form the AppServer.

The first parameter is as usual the AppServer partition. If you want to run synchronous and asynchronous AppServer calls in parallel on the same session-managed AppServer it is required to use a separate connection to that AppServer.

As the default AppServerServiceManager (as-utils.w) will share the same physical connect for two different connection entries in appsrvtt.d as long as the connection parameters are identically you can combine entries with the classic AppServer adressing schema and the URL addressing in appsrvtt.d:

"Default" "localhost" "" yes no "" "smartcomponents" "A" "" ""
"Async" "localhost" "" yes no "" "" "A" "AppServer://localhost:5162?AppService=smartcomponents" ""