Asynchronous functions

<< Click to Display Table of Contents >>

Navigation:  Script Language >

Asynchronous functions

Library has several build-in functions providing asynchronous code execution.

 

Asynchronous execution

 

procedure Async(AsyncFunction, AfterFunction);
 
AsyncFunction: function: variant;
AfterFunction: procedure(Value: variant);

 

AsyncFunction is executed in separate thread. When execution is finished, AfterFuncion is executed in a main thread context, and result of AsyncFunction is passed as AfterFunction parameter.

 

Passing parameters

 

Asynchronous function can access global variables, but they can be changed in a main thread while asynchronous function is executed. Values can be passed directly to both AsyncFunction and AfterFunction using third parameter of Async. Example:

 

Async(
  function(n: integer) begin Result := n + 1 end
  function(n, res: integer) begin ... end
  [123]
);

 

Both functions should have same set of parameters, but AfterFunc also have additional parameter for passing AsyncFunction result.

 

Delayed execution

 

Similarly to Javascript SetTimeout, there is

 

procedure SetTimeout(AFunction: procedure; Timeout: integer);

 

AFunction is executed in a main thread context after Timeout milliseconds.

 

Timer can be reset using ClearInterval function. Example:

 

t := SetTimeout(MyFunc, 500);

ClearInterval(t);