Implement the Async framework

Completed

Long-running processes can time out in finance and operations apps. To overcome this issue, asynchronous processing lets users continue working while the process runs in the background.

For example, you might need to run processing in the application that deals with a large amount of data or transactions, and you don’t want to interrupt daily business processes. Therefore, you can use the asynchronous framework to run these processes in the background while users continue their work without interruption.

To run a process asynchronously, you need to use the runAsync methods from the FormRun and Global classes to call the static method that you want to run. If the process is run on the client side, we recommend that you use the FormRun class's runAsync method. The Global class has a runAsync method that we recommend for running code outside of the client.

You can use a callback method to display a message when the process is completed. Only the FormRun class runAsync method displays the message immediately. If you use the Global class runAsync method, you need to refresh the page.

The static method’s signature must:

  • Be static.
  • Return a container as an output parameter.
  • Have a container for in-parameters. The container can hold more values if needed.
  • Have a cancellation token for in-parameters.

The following example shows a static method that you can use as runAsync :

    /// <summary>
    /// Create entities asynchronously
    /// </summary>
    /// <param name = "_params">Parameters</param>
    /// <param name = "_cancellationToken">CancellationToken object</param>
    /// <returns></returns>
    static container runAsyncJob(container _params, System.Threading.CancellationToken _cancellationToken)
    {
        System.Exception _ex;

        try
        {
            // do something
        }
        catch(_ex)
        {
            throw error("Unsuccessfull");
        }

        return ["Successfull"];
    }

You can use another static method for the result, which is successful or an exception, and its signature must:

  • Be static.
  • Return void.
  • Have AsyncTaskResult as an in-parameter.

The following example is of a static method for a result:

    /// <summary>
    /// Async callback
    /// </summary>
    /// <param name = "taskResult">Result of async import or export processing</param>
    static public void processAsyncDataExecutionResult(AsyncTaskResult taskResult)
    {
        container returnValue;
        System.Exception exception;

        if (taskResult != null)
        {
            // Return value from async method
            returnValue = taskResult.getResult();

            // Exception thrown by async method invocation.
            exception = taskResult.getException();

            if (ClrInterop::isNull(exception))
            {
                info(con2Str(returnValue));
            }
            else
            {
                error(exception.Message);
            }
        }
    }

Now, the runAsync function can use the two methods (functions without a class name is defined in global class).

    public static void main(Args _args)
    {
        System.Threading.CancellationTokenSource tokenSource = new System.Threading.CancellationTokenSource();
        
        // run job asynchronously
        
        runAsync(classNum(MyASyncClass),
                 staticMethodStr(MyASyncClass, runAsyncJob),
                 conNull(),
                 tokenSource.Token,
                 classNum(MyASyncClass),
                 staticMethodStr(MyASyncClass, processAsyncDataExecutionResult));
    }

The parameters that the runAsync function uses must have the following components:

  • Class number for the class to be run.
  • Static method that runs asynchronously.
  • Container for in-parameters. The value from the container parameters is set as a parameter list for the implementation method.
  • Fallback token. The value from the fallback token parameter is set as a token for the implementation method.
  • Class number for the return result.
  • Static method for the result.

To review an example of the preceding information in a standard application, we recommend that you review the Refresh entity list function in finance and operations apps, Data management workspace, Framework parameters, and Entity settings.