ADVANCED: ASYNC

Top  Previous  Next

The ASYNC option can be used in the declaration of a function block.

 

When a program calls a function block without the ASYNC option, the execution of the code in the function block is said to be synchronous. That is, the calling program will first gain control again when the called function block is "finished" executing. If the option ASYNC is used in the declaration of a function block, the execution is said to be asynchronous.

 

When the calling program calls the function block, the execution of the function block is initiated as a separate thread. The calling program immediately regains the control and continues to execute its code. While it does so, the function block will also continue to execute its own code. If the calling program needs to know when the function block is finished executing, the function block needs a way of informing the calling program of this. This could be accomplished, for example, by using a variable in the function blocks VAR_OUTPUT section.

 

The execution of the asynchronous function blocks is done sequentially in the order of execution. This means that while one asynchronous function block is executing, a call to another asynchronous function block will be queued for execution until the currently executing function block terminates.

 

Instead of using an ASYNC function block, it is highly recommended to use the true multithreading.

 

 

The ASYNC option is used like this:

 

FUNCTION_BLOCK ASYNC test;
...
END_FUNCTION_BLOCK;