Waiting for Asynchronous Method Calls to Complete

When the asynchronous method call is executing in the worker application, the application that originated the request continues to execute instructions following the invocation of the asynchronous method call.

The Process class waitForMethods method enables an application to wait for the completion of a number of asynchronous method calls and obtain the results of the calls without having to return to an idle state. It is sometimes necessary to wait for the asynchronous calls to complete before other actions are taken.

A method context object whose state property is not State_Processing is ignored; that is, if it has not yet been invoked or has already completed. The state of a method context object is set to State_Completed when the context is returned as the result of the waitForMethods method.

A maximum of 64 processing method context objects can be passed to the waitForMethods method.

The following diagram shows a period of parallel processing when the asynchronous method call is executing at the same time as the method in the originating application. The originating application then calls the waitForMethods method, which suspends processing until the asynchronous method call completes (or times out). At that point, the originating application resumes processing.

The following code fragment shows the use of the waitForMethods method in this simple scenario.

vars
    context: JadeMethodContext;
    cust: Customer;
    date: Date;
    completedContext: JadeMethodContext;
begin
    ...
    // One asynchronous method call is made
    context.invoke(cust, getHistory, date);
    ...
    // Wait for the asynchronous method call to complete
    completedContext := process.waitForMethods(context);
    write completedContext = context;  // Outputs true
    ...

In the following diagram, the originating application makes two asynchronous method calls. The originating application then makes repeated calls to the waitForMethods method to receive information from the worker applications as the asynchronous method calls complete.

The first time around the loop, the waitForMethods method immediately returns context2, as it has already completed. The second time around the loop, it suspends processing until context1 is completed. The loop is then terminated, because the waitForMethods method returns a null reference, indicating that all asynchronous method calls have completed.

The following code fragment shows the use of the waitForMethods method in this more-complicated scenario.

vars
    context1, context2: JadeMethodContext;
    cust1, cust2: Customer;
    date1, date2: Date;
    completedContext: JadeMethodContext;
begin
    ...
    // Two asynchronous method calls are made
    context1.invoke(cust1, getHistory, date1);
    context2.invoke(cust2, getHistory, date2);
    ...
    completedContext := process.waitForMethods(context1, context2);
    // Loop through contexts as they finish
    while completedContext <> null do
        write completedContext;
        completedContext := process.waitForMethods(context1, context2);
    endwhile;
    ...