How to start selected scripts in another thread

All scripts run in one thread (Main thread) in the PROMOTIC application by default. It means, for example, that if one script runs, then other scripts must wait for ending the running script. Therefore it isn't possible, for example, to create infinite loop (e.g. by means of the VBScript statements Do...Loop, For...Next, etc.) for testing a variable and to end the loop after the variable gets the required value. This approach with infinite testing is not allowed in the PROMOTIC system!

It also means that, for example, after you start the synchronous method Pm.FileCopy in the script for copying a large file on the network disk, then this method doesn't end (and thus the active script doesn't) until the file has been actually copied! Other scripts don't run for all the time!

To solve such problem therefore it is allowed to start scripts in another thread. It can be performed only with the help of the PmSequencer object. If there is selected Normal, BelowNormal or AboveNormal in the Used thread configuration of this object on the Sequencer configuration page, then the onStep event of this object will be started with normal, lower or higher priority in the thread. Default values can be moved to this thread by means of the parameters Val1, Val2 and Val3 of the PmSequencer.Add method. Then the script can work in the user thread with these values (any time and with scripts running in the Main thread in parallel). From the user thread it is also possible to start scripts in the Main thread by calling methods of the PmFolder object - see the PmObject.Methods property.
Caution! If another thread is used then an access to other objects from the script proceeds in other than the main thread and it is necessary to take into account possible problems with the synchronization on reading and writing into Promotic objects. This option is suitable first of all for time consuming calculations on the background.
The problem with synchronization (consistency) of read and write of the data (when using a single thread) can be solved by several ways. One of the basic procedures is to divide the whole procedure into three steps. Read the data from the application, process the data and write the output data into the application (each step is initialised by the onStep event). The read and write step may be assigned to the main thread (preventing the data synchronization problem) and the data processing step can be assigned to the work thread. The assigning of processes to threads can be defined by the Params metody PmSequencer.Add parameter. The data read and write then runs in the main thread and data procesing runs in the work thread not delaying the main thread. The processed data must be transmitted between each step. The easiest way of data sharing is to create in the script of the step (onStep event) a complementary array and transmit it as a parameter Val2 of the PmSequencer.Add method. It is also possible to transmit the data in the PmData object. Usage see The example of time consuming process in a work thread.
© MICROSYS, spol. s r. o.Tavičská 845/21 703 00 Ostrava-Vítkovice