Executing/Switching the Background Task

The Background Task executes scripts from the Math and Scheduler worksheets (for example, messages from Alarm and Trend worksheets). In addition, the Background Task executes all Recipe and Report commands when the Recipe() or Report() functions are executed during the runtime.

Although the Alarm, Math, Scheduler, and Trend tasks are not threads, you can specify or change their Period time in the Program Settings.ini file located in the IWS program directory.

The Period default values (in milliseconds) are as follows:
[Period]
Math=100
Sched=50
Alarm=100
Trend=1000

These values mean that every 100ms, IWS generates a Period message to the Math task. Every 50ms, IWS generates a Period message to the Scheduler task, and so on.

CAUTION:
We strongly recommend that you do not modify the Background Task default values unless it is absolutely necessary. Configuring any of these parameters incorrectly can cause your entire system to malfunction (for example, CPU usage will go to 100%) and/or cause some tasks to perform poorly.

If you must change the parameter defaults, note the values before making your changes so if a malfunction occurs you can return to the original settings.

Keep in mind that the Background Task thread has the same priority as other threads in IWS (Drivers, Viewer, and so forth), which means that the operating system will not execute this task continuously for more than 20ms.

The Background Task executes the Recipe and Report worksheets when the Recipe and Report functions are called, respectively. Because the Recipe() and Report() functions are synchronous, once the Background Task starts executing the functions, it will not switch to another task (Alarm, Math, Scheduler, or Trend) until it completely executes the functions. Executing a Recipe() or Report() function usually takes a few milliseconds.

The Background Task must switch between the Alarm, Math, Scheduler, and Trend tasks. When Background Task switches to the Scheduler task, it will not switch to another task (Alarm, Math, or Trend) until all Scheduler worksheets are executed. After executing all Scheduler worksheets, the Background Task will not execute the Scheduler again until it receives the next Period message for the Scheduler task.

The Background Task applies the same behavior when executing the Alarm and Trend tasks — when the Background Task switches to the Alarm or Trend task, it will not switch to another task until it handles all pending messages. So, the Background Task will not execute the Alarm or Trend task again, until IWS generates the next Period message for each of these tasks.

The Background Task typically executes the Alarm, Scheduler, and Trend tasks in a few milliseconds. However, it can take longer to execute the Math task because it usually contains loops and complex scripts. Consequently, the mechanism used to execute the Alarm, Scheduler, and Trend tasks cannot be applied to the Math task.

The Background Task executes the Math task for no more than 10ms continuously before switching to other task (such as the Scheduler). The Background Task cannot execute the Math task again for the next 50ms; however, the Background Task can execute other tasks (Alarm, Recipe, Report, Scheduler, or Trend) during this 50ms period. After the Background Task executes all of the Math worksheets, it will not begin a new scan of the Math worksheets until IWS generates a new Period message for the Math task.

It is important to re-emphasize that this process was created to prevent 100% CPU usage all the time.

CAUTION:
We recommend caution when using the Math function in a Scheduler worksheet or for a screen object (such as the Command animation).

When the Scheduler task executes a Math() function, no other task can be executed by the Background Task until the Scheduler executes the entire Math worksheet called by the Math() function. This process can take several milliseconds or even seconds, depending on how you configured the script in the Math worksheet (especially for loops).

If you configure a Math() function for a screen object, the Viewer stops updating the screen until the Viewer executes the entire Math worksheet called by the Math() function.

If you must use the Math() function for the Scheduler task or a screen object, we recommend using the following procedure to prevent process delays:
  1. Specify one auxiliary tag with the value 1 and the Scheduler or Viewer task will send a message to the Tags database to update this tag value.
  2. Configure the tag in the Execution field of the Math worksheet to be executed. When the Background Task scans the Math worksheet, IWS will execute the worksheet.
  3. Reset the tag in the last line of the Math worksheet (write the value 0 to the auxiliary tag).

As a result, the Background Task will not execute the Math worksheet in the next scan unless the auxiliary tag is set to the value 1 again.