Promotic

How to start selected scripts in another thread

All scripts run in one thread (in the main thread) in the PROMOTIC application usually. It means that, for example, 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 statements do...while 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, if for example, after you start the synchronous Pm.FileCopy method 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 by the PmaSequencer object. If there is selected Normal, BelowNormal or AboveNormal in the "Used thread" configurator in this object on the "Sequencer" tab, then the onStep event of this object will be started with normal, lower or higher priority in the thread. Initial values can be moved to this thread by means of parameters Val1, Val2 and Val3 of the PmaSequencer.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 the methods of the PmaFolder object - see the PmaObject.Methods property.

Caution! If another thread is used, then an access to other objects in the script proceeds in other than in the main thread and it is necessary to take into account possible problems with the synchronization on reading and writing into Pma 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. That is reading the data from the application, process the data and write the output data into the application (each step is initialised by triggering the onStep event). The read and write step may be invoked in the main thread (preventing the data synchronization problem) and the data processing step can be invoked in the worker thread. The assigning of processes to threads can be defined in the Params parameter of the PmaSequencer.Add method. 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 (the onStep event) a complementary array and transmit it as a Val2 parameter of the PmaSequencer.Add method. It is also possible to transmit the data in the PmaData object. The usage see Example of time consuming process in the worker thread.
See also:
- PmaSequencer (object)
- PmaSequencer.Add (method)
- Pm.AppPriority (property)
PROMOTIC 9.0.27 SCADA system documentation MICROSYS, spol. s r.o.

Send page remarkContact responsible person
Navigation:
 
 
- How to start selected scripts in another thread
 
 
- SVG
© MICROSYS, spol. s r.o.