Futurebasic/Language/Reference/threadbegin

From Wikibooks, open books for an open world
Jump to navigation Jump to search

THREADBEGIN[edit | edit source]

Statement[edit | edit source]

(+) Appearance (+) Standard (+) Console

Syntax[edit | edit source]

THREADBEGIN FN name [, parameter& [, stackMin&]] === Revised === August, 2002 (Release 7)

Description[edit | edit source]

What exactly is a thread? It is a function that performs a task. The difference between a threaded function and any other function is that the threaded version runs in the background. Your program (and other running processes) advance without interruption as the threaded task is performed. There are many reasons for using threaded functions. One that is becoming popular relates to functions that access Internet connections. Since logging in and downloading often take a good bit of time, it makes sense to hand such a task to a threaded function and go on about your business. You might handle other tasks like complex calculations, lengthy sorts, or time-consuming file loads. When a threaded function is called, it executes without stopping until it is complete—just like any other function. But at some point inside of the function, you yield to other processes by calling THREADSTATUS. The THREADSTATUS statement allows other actions to take place and optionally sets a timed delay for the number of ticks that should elapse before your function regains control. The parameter passed to a thread may be any long integer required by your program. It is for your use. If a parameter is sent, you should accept it in the named local function. For instance, if a parameter were used in the example below, we would create the function with LOCAL FN myThread(param AS LONG) instead of just plain LOCAL FN myThread. If the stackMin parameter is omitted, FutureBASIC sets a minimum stack space of 131072 bytes (128K). You may experiment with larger or smaller values for your specific needs. Example: This simple example creates a thread that prints a string of text, piece by piece, in a FOR/NEXT loop. The THREADSTATUS call installs a callback time of 10 ticks which makes the text appear slowly. You can type into the edit field as the threaded text is placed on the screen. LOCAL FN myThread

   
DIM t$,x, abort
   t$ = "Hello. I am a thread. "
   t$ += "I execute in the background."
   FOR X = 1 TO LEN(t$)
PRINT @(1,1) LEFT$(t$,x);
abort = THREADSTATUS(10)//call me in 10 ticks
IF abort != 0 THEN EXIT FN
   NEXT
END FN WINDOW 1
TEXT _sysfont,12,0,0
PRINT@(1,3)"Type into the edit field"
EDIT FIELD 1,"",(8,80)-(200,100)
THREADBEGIN FN myThread
DO
   HANDLEEVENTS
UNTIL 0

THE ABOVE SECTION IS EXTREMELY CONFUSING AND STILL NEEDS FIXING!!!

See Also[edit | edit source]

THREADSTATUS; TIMER; ON TIMER;