Thread

(Engine-Level Function)

Description: Launches a module in its own separate thread.
Returns: The object value of the instance of the module that is launched into the new thread.
Usage: Script Only.
Function Groups: Basic Module
Related to: Call | FindVariable | Launch | LoadModule | ThreadHistory | ThreadList | ThreadName
Format: Thread(Module, Parent, Caller, Name, P1, P2, ...)
Parameters:  
Module
Required. A module pointer or a text expression giving the module to run. If the module is in scope, the text expression giving its name may be used, otherwise the module pointer returned from either a LoadModule or FindVariable statement is typically used.
Parent
Required. The object value of the module where Module is to resolve its global variable references. If a valid non-object value is supplied Module will resolve its global variable references to the scope defined by the first parameter. If this is invalid, the module will still run, but global references will be invalid.
Caller
Required. The object value of the window to draw in. This specifies the module instance where Module acts as if it were called from. If this is invalid, the module will still run but will not stop without a Slay. If it is valid, the module will stop when the Caller module instance stops or when a Slay is executed upon it.
Name
Required. Any text expression giving the name that is to be associated with this particular thread. This is the name that will be returned by the ThreadList function.
P1, P2, ...
Required. Are any expressions which will be supplied as parameters to the module.
Comments: This function behaves similar to Launch except that a separate thread is created in which the module is executed. This means that it will not block execution of other modules; the CPU time will be divided equally amongst threads of equal priority. Great care should be exercised when using this function since each thread created by Windows™ uses certain system resources and will by its very existence slightly slow the running of the application. In general, the Thread function should only be used when a module would otherwise monopolize system resources to such an extent that other critical modules would be severely hampered in their execution.

This function returns an object value of the newly started module, in the same way that Launch does. This means that parameters are passed to the module as a value only, and if the module instance changes one of their values, its value will not change outside of the scope of the threaded module. Variables external to the module that the module itself will be required to alter should reside within the scope of Parent and be set directly, rather than passed as parameters.

Example:

If ! Valid(ptr);
[
  ptr = Thread("CheckTank" { Module to launch },
               Self(), Self() { Parent and caller },
               CheckTankThread { Name },
               pressure1, pressure2, level { Parameters });
]