Written by Denis Cavalcanti, Developer at InduSoft
Why use Asynchronous Functions in InduSoft Web Studio
When you run a VBScript function in InduSoft Web Studio, it runs in the main thread of a process. This means when you run a VBScript function, the whole process is blocked until the function ends. Because of that the application appears to be locked or blocked. Running VBScript asynchronously prevents this from happening.
When you perform an asynchronous function, it creates its own thread, separated from the main thread. So, while running a function in VBScript, the main thread is not blocked and continues running and executes all messages and events. VBScript does not natively have any methods for launching separate threads; they are executed in a single thread. Some applications require the execution of time consuming scripts. Since the VBScript task is single-threaded, the execution of such scripts compromises the performance of the remaining script algorithms of the project. Running the script asynchronously will prevent this.
Synchronous x Asynchronous
A synchronous function means that whoever called the function should wait for feedback before proceeding. This is the most common way to call any function. In an asynchronous call, the function call does not expect a return before proceeding, keeping more than a “run line” in the code.
Some functions take long time to execute. Typical time-consuming functions are:
- Database operations, like create, update, insert, and delete.
- Updates to a large number of tags, searches for values, and huge loops.
- I/O operations, such as reading and writing files.
- Complex VBScripts.
Running asynchronously prevents problems such as:
- The application seems to be locked
- Other operations cannot be performed
- The graphical interface is locked
Because of the problems in running synchronous functions, Indusoft Web Studio v7.0+SP1 implements the ability to execute VBScripts asynchronously. With this function we have a modern and effective approach to scripting. Most computer systems are actually multi-tasking among multiple threads or programs already, and this technique allows a speed-up of the overall system for a meaningful performance gain.
New Functions in Indusoft Web Studio v7.0+SP1
Indusoft Web Studio v7.0+SP1 implemented two new functions to perform asynchronous VBScripts functions:
This function is similar to RunGlobalProcedureOnServer. The difference is that when it runs, a new thread is created, separated from the main thread.
This is ideal for a math worksheet or a scheduler task;
$RunGlobalProcedureAsync(“MyFunctionVBScript”, arg1, arg2,…)
The VBScript function must be declared a GlobalProcedure. You can create a Sub or a Function in VBScript. Remember that the Sub doesn’t has a return, and Functions return a value. The
RunGlobalProcedureAsync function returns the thread ID created. With this ID you can retrieve information about the thread using the other function RunGlobalProcedureAsyncGetStatus.
You should use this function alongside RunGlobalProcedureAsync. It is necessary to monitor the status of threads that are running or monitor a specific thread.
Here is an example of how to use this function in InduSoft Web Studio:
When no parameters are set, this function returns the number of threads running
With a parameter, it returns the status of the thread, if ID exists. In this case it returns if already completed, if it is running, or if there was an error or return value of the function.
$ RunGlobalProcedureAsyncGetStatus(“arrayTagThreadIds”, “arrayTagStatus”, “arrayTagParameters”)
In this case the three parameters are required. You need to declare a tag array to store the IDs of threads, a tag array to store their status and a tag array to store parameters of each function performed. Running RunGlobalProcedureAsyncGetStatus, is checked all the threads that are executing and their status. Case successful, if a thread is finished and your status is completed it is removed from the list of threads and new threads can be created.
For more examples, details and information see Technical Reference in Indusoft Web Studio v7.0+SP1.