Legato
Legato

GoFiler Legato Script Reference

 

Legato v 1.5d

Application v 5.25a

  

 

Chapter FourFlow Control (continued)

4.14 Control and Background Processes

4.14.1 Overview

Legato allows the execution of shell commands and also to create processes, many of which will run in their own independent threads. In addition, Legato scripts can be run in one or more separate threads. These are known as background scripts.

Normally, when a script is run the script engine is created and the script is run in the primary or foreground thread. A computer can run many separate programs at once and programs themselves can run additional sections simultaneously. In actuality, a computer can only run as many thread processes at the exact same time as the computer has “cores” or processors. While a separate thread is started, it usually is managed by the operating system to allow it to share resources, including CPU cycles, memory and other hardware. Even a single core computer can perform hundreds of tasks seemingly simultaneously by carefully sharing resources, queue and ordering requests and giving each task or thread the time it needs to perform its work.

There are some differences between foreground and background threads. For programs employing a window (which most Windows program do), only the primary thread associated with the window can dispatch messages. Windows messaging can sometimes give the illusion of multiple threads when in fact the nature of message based processing actually slices up certain activities or messages. For example, moving the mouse generates hundreds of mouse move messages in the foreground. At the same time a timer message can be dispatched to perform some unrelated task. However, should the processing of that timer message take too long, or if some other message processing takes too long, mouse movement can appear jerky or unresponsive.

When a background thread is employed, it can run independently, and, if it takes too long to complete an action, the system will simply “switch it out” for the primary thread or another thread. As a typical computer runs, hundreds of tasks are constantly being queued, suspended, executed, started and stopped. Listening to a network connection, refreshing video, processing and directing keyboard strokes, timing activities all run in separate threads.

Using background threads can open a new vista of programming opportunity, flexibility and complexity. It can also make for very tangled and difficult problems if one is not careful in the design or failure to recognized certain problems.

A background script can be run to perform simple tasks. For example:

        int             ix;
        string          s1;
        
        while (ix < 30) {
          s1 = GetLocalTime(DS_ISO_8601);
          StatusBarMessage("Time: %s - %d", s1, ix);
          Sleep(1000);
          ix++;
          }
        
        MessageBox("Thread Completed");

If run in the foreground, the script will tickle the status bar with the date and time and a loop count. However, the program is essentially useless to the user. When run in the background, other tasks can be performed by the user (with the exception of clashing status bar messages):

        handle             hThread;

        hThread = RunBackgroundScript("D:\\MyScripts\\Background.ls");

The RunBackgroundScript function will run the script and will continue to execute the script in the background until it is forced to stop or the program ends on it own accord. 

While running, the thread becomes one of many being executed by the application. Some may be Legato scripts, some embedded in the application and others spawned by Windows or its components. For example (from Process Explorer):

Notice the highlighted entry showing the DLL psg09 with ‘run_script’ and ‘script_manager’. The top entry is the foreground or primary thread. Also notice there are many other threads running. If a file is opened using the file browse or an Internet connection established, the number of background threads increases dramatically. These also disappear as their execution completes.

4.14.2 Limitations

While a script is running as a background thread, modal dialogs cannot be used.

Only the basic message box can be displayed using the MessageBox function which immediately returns without waiting for a response from the user.

4.14.3 Running Scripts in Background

The primary way to run a background script is to use the RunBackgroundScript function.

4.14.4 Functions