Class Thread() Foundation

Class function of the Thread class.

Description

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.

Programming using Thread objects, and creating threads in general, requires careful encapsulation of the program components which are to run in separate threads. Programmers that have never written programs for multi-threading should read the chapter "Multi-tasking and Multi-threading" in the Xbase++ documentation before using thread objects.

Class methods
:new()
Creates an instance of the Thread class.
Instance variables
:active
Indicates whether program code is being executed in a thread.
:atEnd
Optionally specifies program code to execute once at the end of a thread.
:atStart
Optionally specifies program code to execute once at the beginning of a thread.
:cargo
Instance variable for ad-hoc use.
:debug
Specifies whether the thread can be debugged.
:deltaTime
Elapsed time since the last restart of a thread.
:interval
Time interval for repeated execution of program code in a thread.
:name
The name of the Thread
:priority
Identifies the priority of a thread.
:result
RETURN value of a thread.
:startCount
Indicates the number of times the execution of program code has started in a thread.
:startTime
Time when a thread starts executing program code.
:threadID
Numeric ID of a thread.
Methods
:atEnd()
Reserved method for use by subclasses of the Thread class.
:atStart()
Reserved method for use by subclasses of the Thread class.
:execute()
Reserved method for use by subclasses of the Thread class.
:quit()
Terminate the thread of this thread object
:setInterval()
Sets the time interval for restarting a thread.
:setPriority()
Sets the priority of a thread.
:setStartTime()
Defines the start time for a thread to execute program code.
:start()
Executes program code in a thread.
:synchronize()
Synchronizes a second thread with the current thread.
Examples
Allowing a program to be run in a separate thread

// 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 

Terminating a thread from outside

// 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 
Feedback

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.