Class Thread() Foundation
Class function of the Thread class.
The Thread class provides the Xbase++ programmer with a powerful tool for use in accessing the multi-threading capabilities of the operating system and for allowing an application program to run in multiple threads. Within various threads, different parts of an application run at the same time. An Xbase++ application is always executed in several threads. However, the actual program code written and compiled using Xbase++ normally runs in only one thread while other threads are used for internal purposes, like garbage collection, for example. Using Thread objects, additional threads for program code can be created so that an Xbase++ application can be divided up into different program components that execute independently.
// The example demonstrates how a new thread in which
// part of a program runs is created. A Thread object
// runs code to display the time while data is entered
// in the main thread.
PROCEDURE Main
LOCAL oThread := Thread():new()
CLS
oThread:start( "showTime", MaxRow(), MaxCol()-7 )
USE Customer NEW EXCLUSIVE
@ 10,10 SAY " Lastname" GET FIELD->Lastname
@ 12,10 SAY "Firstname" GET FIELD->Firstname
READ
RETURN
PROCEDURE ShowTime( nRow, nCol )
DO WHILE .T.
DispOutAt( nRow, nCol, Time() )
Sleep(100)
ENDDO
RETURN
// The example shows a user-defined Thread class with methods
// suitable for terminating a thread from outside.
CLASS MyThread FROM Thread
PROTECTED:
VAR terminated
EXPORTED:
VAR workCounter
INLINE METHOD init
::Thread:init()
::terminated := .F.
::workCounter := 0
RETURN self
INLINE METHOD terminate
RETURN ( ::terminated := .T. )
METHOD execute, checkTermination
ENDCLASS
** This method is executed in the thread
METHOD MyThread:execute
DO WHILE ::workCounter < 10000000
// program code for thread
DispOutAt( 0, 0, ::workCounter++ )
::checkTermination()
ENDDO
RETURN self
** This method checks whether or not the thread must
** terminate and cancels it, if necessary
METHOD MyThread:checkTermination
IF ::terminated
::terminated := .F.
::quit()
ENDIF
RETURN self
** Procedure for testing the MyThread() class
PROCEDURE Main
LOCAL oThread := MyThread():new()
LOCAL cVar := "Hello World"
CLS
oThread:start()
@ 10, 10 SAY "Hi:" GET cVar
READ
oThread:terminate()
WAIT "Thread is terminated from the Main thread"
RETURN
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.