How to Work with Background Procedures

Navigation:  »No topics above this level«

How to Work with Background Procedures

Previous pageReturn to chapter overviewNext page

 

Overview

 

MCL-Designer's typical workflow (using programs, global/local procedures, routines, screens, etc.) is executed in the foreground, meaning, it uses a main thread.

A Background Procedure is similar to a standard program except it, specifically, runs in the background, parallel to the foreground workflow, with the use of a secondary thread.

 

Background procedures are suited for:

potentially time consuming operations (asynchronous execution) that do NOT require operator interaction - interface elements such as screens and controls or processes that call interface elements (ex: the processes of the Display Controls group) are NOT available for background procedures.

operations executed as "application services", meaning, operations that handle specific/recurring tasks related to application management such as the automatic archiving/deleting of old files or querying the host on a regular basis.

operations that manage the application/device's interaction with external hardware, meaning, the background procedure acts as a "device driver". Ex: A background procedure in charge of the printing of a label by an external printer.

 

Background procedures are always called by a program in the foreground with the use of the Branch process "Call Background Proc.".

 

Check the examples of Background Procedures provided at the end of the topic.

 

Custom Actions

This type of action can be used by background procedures to share data with the foreground. You can create as many custom actions as necessary in your project and use them in the required background procedure(s). You can also have multiple background procedures feed data to the same custom action. See Custom Actions.

This type of action includes its own parameters.

 

Operationally speaking, the use of a custom action within a background procedure requires the following:

 

At Background Level

1. Define the necessary custom action(s). You can access the available custom action list by clicking , whenever available (ex: in the properties window of the "Trigger Action" process).

See Custom Actions.

2. Create the required background procedure(s).

3. Within the intended background procedure, use a "Trigger Action" Branch process to associate the custom action to its execution and inform the foreground that the declared custom action is available with data. (At the same time, this process can also be used to set values for the custom action's parameters.)

 

At Foreground Level

 

4. Use the "Call Background Proc" process in the intended program to initiate the required background procedure/assign input values to it.

5. Add the required custom action to a screen (click in a screen's "Actions" tab and, in the resulting window, select the intended custom action from the drop-down).

 

Once the "Trigger Action" process is executed in the background, the foreground program with the added custom action in one of its screens will retrieve the custom action's data and trigger its own business logic (with the use of processes that will handle the data received from the background execution).

If required by the nature/purpose of the custom actions, it is possible to queue them so a custom action is NOT replaced by a newly generated custom action and you lose its data.

 

 

Exchanging Data via Custom Actions

 

A custom action is a way for background procedures to interact with the foreground - it shares information with the running foreground program. Meaning, a custom action can be associated to/started by a background procedure via a "Trigger Action" process which also notifies the foreground that there is information to share. In turn, the foreground retrieves that information with the use of a screen that references that custom action. See To Add a Custom Action to a Screen

 

 

Exchanging Data via Background Variables

 

Background procedures use Background variables to exchange data between the background procedure and the program running in the foreground. See Background Variables.

They have Global ("G_"), Program ("P_"), App Params ("A_)" and System ("X_") variables at their disposal.

Background procedures work with input values provided by the calling program via the "Call Background Proc." process. Input values are stored/assigned to a specific type of Background variable - an Input Background variable.

This type of Background variable is created within the background procedure's properties window, specifically, the "Input" tab. See Adding a Background Procedure.

The output values of a background procedure are stored into Output Background variables and they are retrieved by the program running in the foreground with the use of any process that can access the content of a variable.

Output Background variables are created in the "Output" tab of a background procedure's properties window. See Adding a Background Procedure.

Background variables encompass Input and Output variables but it is also possible to create generic Background variables to store other types of values - if you access a "Variable Select" window outside the "Input" and "Output" tabs, the defined variable will not have a specific type. See To Select/Create a Variable.

Each background procedure uses its own Program variables and they cannot be shared with other background procedures or other foreground running programs.

 

Background Procedure Processes

 

There are specific processes that deal with background procedures. Some are integrated in the Branch group of processes, others in the Variable group of processes.

 

The "Call Background Proc" process - is used to call the intended background procedure and assign input values to it. It is a Branch process. SeeWorking with Call Background Process. It is ONLY available outside the "Background Procedures" module - it can only be used in the foreground.

 

The "Check Procedure Status" process - is used by a foreground program to verify if the background procedure is running or not. It is a Branch process that is NOT available in the "Background Procedures" module. See Working with Check Procedure Status Process.

 

The "Wait for Variable Update" process - is a Branch process that is used by the background procedure to check if its variables have been updated by the foreground program(s) and set its own workflow accordingly. This process is ONLY available within the "Background Procedures" module. See Working with Wait for Variable Update Process.

 

The "Set Background Var" process - this is a Variable process that assigns output values and/or sets other types of value into generic Background variables. It can be used within the "Background Procedures" module but also in the foreground, meaning, the main thread can also assign values to the Background variables. See Working with Set Background Var Process.

 

The "Trigger Action" process - is a Branch process that is used to start the defined custom action within the intended background procedure's context and, if necessary, to assign values to its parameters.

It is ONLY available in the "Background Procedures" module. See Working with Trigger Action Process.

 

Background procedures can be reused in other MCL-Designer projects. The background procedure is exported into the Library (an MCL-Designer folder that stores exportable elements such as data files, programs, global events, screens, etc.) and, when required, it is imported (from Library) into another project or projects.

See To Export a Background Procedure to Library  and To Import a Background Procedure from Library).

 

 

Background Procedure Usage/Recommendations

 

When developing a project with background procedures, consider the following:

 

An application can run multiple background procedures - each one uses its own secondary thread.

The use of several background procedures (secondary threads) affects the target device's performance. The developer must analyze/balance the quantity of background procedures and the target device's capabilities (ex: memory, processing) so as to maintain maximum performance.

 

A background procedure CANNOT call other background procedures.

 

A specific background procedure has to be stopped prior to being called again.

 

All background procedures must be explicitly finished before exiting/quitting the application (we recommend the programming of a "graceful exit"). See Examples of Background Procedures.

 

A background procedure uses its own variables (Background variables identified with a "B_") but it can also use global ("G_"), program ("P_"), system ("X_") variables and app params ("A_").

 

Background variables are mainly used to communicate with the foreground application but global, system variables and app params can be shared with other background procedures/programs.

In the case of global variables, we recommend the use of background variables instead, to avoid potential conflicts after the export/import of a background procedure onto another project that uses a global variable with the same name but with a different purpose.

 

It is possible to retrieve the background variable values of a background procedure that has been stopped. However, if a background procedure is restarted, the background variables are reset, meaning, the values stored during the previous execution are lost and the variables have their initial values restored.

 

Profiles created within a background procedure (ex: Host, Aux, Web Service) are also available for the programs running in the foreground and vice versa.

 

Besides the Background variables, background procedures can also include custom actions to notify/share data with the foreground application. You can associate specific custom actions to a background procedure that, when called by a program, can trigger the execution of something in the foreground. Ex: Associate/start a custom action in a background procedure (via the "Trigger Action" process) to notify when the device is located on a specific GPS coordinate. Add this custom action to a screen of the required foreground program to retrieve the custom action's notification and trigger the workflow (processes) defined for that screen.

 

Some process groups/processes are NOT available for background procedures because:

 

they are used by the foreground to call/check the intended background procedure (the "Call Background Proc" and "Check Procedure Status" Branch processes).

 

they represent operator interface. Ex: The "Display Controls" and "Control Operations" process groups affect controls which cannot be applied onto background procedures and the device's camera related processes are also grayed out.  

 

they call/reference elements that only run in the foreground such as global events (ex: the "Start/Resume", "Reset" and "Suspend" processes related to global events).

 

 

The "Background Procedures" Right-Click Menus

 

Background procedures are managed in the "Procedures" module - this module includes the project's Global/Background Procedures and its Global Events. Right-clicking the background procedures section/elements provides the options to manage them:

 

Right-clicking an empty area within the "Procedures" module provides a menu with general options:

 

Add Global Procedure

Use it to add a global procedure. See Global Procedures.

Add Background Procedure

Adds a background procedure. See Adding a Background Procedure.

Add Global Event

Adds a global event. See How to Work with Global Events.

Library Import

Click "Library Import" to open the "Library Manager" window so you can import the intended element into your current project.

If you intend to import a background procedure, see To Import a Background Procedure from Library.

 

Right-clicking "Background Procedures" (the name of the section that displays the existing background procedures) provides the following related options:

 

Add Background Procedure

Adds a background procedure. See Adding a Background Procedure.

Library Import

Click "Library Import" to open the "Library Manager" window so you can import a background procedure into your current project. See To Import a Background Procedure from Library.

 

If you right-click an existing background procedure, you access the following options:

 

Edit

Opens the selected background procedure's properties window so you can edit it. See Editing a Background Procedure.

Copy

Copies the selected background procedure.

Use the "Paste" option in the right-click menu of the destination to complete the operation.

Paste

Pastes the previously copied background procedure. This option is only available after a "copy" operation takes place.

Duplicate Background Procedure

Duplicates the selected background procedure with all its features and processes. The duplicate has the same name as the original background procedure, plus an incremented number. See Duplicating a Background Procedure.

Delete

Deletes the selected background procedure. See Deleting a Background Procedure.

Add Local Procedure

Adds a local procedure to the selected background procedure. See Local Procedures.

Add Routine

Adds a routine to the selected background procedure. See Working with Routines.

Input

Opens the selected background procedure's "Input" tab directly.

Output

Opens the selected background procedure's "Output" tab directly.

Library Export

Use this option to export the selected background procedure. See To Export a Background Procedure.

 

If the selected background procedure is a "locked" background procedure (identified with a ), the "Library Export" option is NOT applicable. "Locked" elements CANNOT be exported - any export attempt leads to an error message.

Library Import

Import a background procedure from "Library". See To Import a Background Procedure.

Search in ...

Opens the "Search" window. See Detail of Search.

 

The "Search in..." option in this right-click menu enables an automatic to search in the selected global procedure.

 

Search 'xxx'

Opens the "Search" window. See Detail of Search.

 

The "Search 'xxx'" option in this right-click menu enables an automatic search of the selected text string in the current project and returns all the occurrences.

 

 

Examples of Background Procedures

 

Click Here for Example 1 (retrieving GPS coordinates, at a specific moment)

 

Click Here for Example 2 (retrieving GPS coordinates during an application session)

 

Click Here for Example 3 (getting new tasks from a Web Service and notifying the foreground application)

 

 

Topics

 

This chapter covers the following topics:

 

Topic

Adding a Background Procedure

Duplicating a Background Procedure

Exporting/Importing a Background Procedure

Editing a Background Procedure

Deleting a Background Procedure