Xbasic

THREAD_CREATE Function

Syntax

V THREAD_CREATE(C thread_name,C Xbasic_code[,P variable])

Arguments

thread_nameCharacter

A character string to be used as the name of the thread. This thread name must be unique among all currently running threads. There will always be a thread named "Main" running, so you cannot use this name.

Xbasic_codeCharacter

The string of code that should be run in this background thread.

variablePointer

A pointer to a namespace you would like to use as the context for the thread. This is similar to passing pVariables into a UDF and then using with pVariables ... end with.

Description

Create a thread - requires a unique thread name , code to run - allows for 'base' variable frame to be passed in.

Discussion

The THREAD_CREATE() function creates a new thread.

XBASIC_ERROR_LOG()is very useful for threading because it will create a log along with some debugging information if or when a thread abnormally terminates. Without this, there is no way to tell why your thread quit.
No user interface related commands are allowed in a thread. Using any command that attempts to create or manipulate any type of user interface from within a thread will cause Alpha Anywhere to hang. Examples of such invalid functions include debug() , ui_*, StatusBar.*, ControlPanel.*, trace.*, etc.

Example 

thread_create("mythread", <<%code%
while flag_run
    ' do a bunch of stuff
end while
%code%)

Then when you want to stop the thread, you can do the following from another script, the interactive window, etc:

tv = thread_variables("mythread")
tv.flag_run = .f.
delete tv

The following script removes unwanted temporary files. This thread works at the lowest priority.

function RaysCleanup as V()
thread_create(thread_name_create("Ray's cleanup"),<<%code%
dim self as P
dim files as C
self = thread.current()
self.set_priority(-2)
files = filefind.get(a5.Get_private_Path() + chr(92) + "$$*.*",0,"pn")
*for_each(x,file.remove(x),files)
files = filefind.get(a5.Get_private_Path() + chr(92) + "*.pdf",0,"pn")
*for_each(x,file.remove(x),files)
files = filefind.get(a5.get_path() + chr(92) + "$$*.*",0,"pn")
*for_each(x,file.remove(x),files)
%code%)
end function

Limitations

Desktop applications only.

See Also