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.

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
Creates an instance of the Thread class.
Instance variables
Indicates whether program code is being executed in a thread.
Optionally specifies program code to execute once at the end of a thread.
Optionally specifies program code to execute once at the beginning of a thread.
Instance variable for ad-hoc use.
Specifies whether the thread can be debugged.
Elapsed time since the last restart of a thread.
Time interval for repeated execution of program code in a thread.
The name of the Thread
Identifies the priority of a thread.
RETURN value of a thread.
Indicates the number of times the execution of program code has started in a thread.
Time when a thread starts executing program code.
Numeric ID of a thread.
Reserved method for use by subclasses of the Thread class.
Reserved method for use by subclasses of the Thread class.
Reserved method for use by subclasses of the Thread class.
Terminate the thread of this thread object
Sets the time interval for restarting a thread.
Sets the priority of a thread.
Defines the start time for a thread to execute program code.
Executes program code in a thread.
Synchronizes a second thread with the current thread.
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. 

   LOCAL oThread := Thread():new() 

   oThread:start( "showTime", MaxRow(), MaxCol()-7 ) 


   @ 10,10 SAY " Lastname"  GET FIELD->Lastname 
   @ 12,10 SAY "Firstname"  GET FIELD->Firstname 


PROCEDURE ShowTime( nRow, nCol ) 
   DO WHILE .T. 
      DispOutAt( nRow, nCol, Time() ) 

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 
      VAR terminated 
      VAR workCounter 

      INLINE METHOD init 
         ::terminated  := .F. 
         ::workCounter := 0 
      RETURN self 

      INLINE METHOD terminate 
      RETURN ( ::terminated := .T. ) 

      METHOD execute, checkTermination 

** This method is executed in the thread 
METHOD MyThread:execute 
   DO WHILE ::workCounter < 10000000 
      // program code for thread 
      DispOutAt( 0, 0, ::workCounter++ ) 
RETURN self 

** This method checks whether or not the thread must 
** terminate and cancels it, if necessary 
METHOD MyThread:checkTermination 
   IF ::terminated 
      ::terminated := .F. 
RETURN self 

** Procedure for testing the MyThread() class 
   LOCAL oThread := MyThread():new() 
   LOCAL cVar    := "Hello World" 


   @ 10, 10 SAY "Hi:" GET cVar 


   WAIT "Thread is terminated from the Main thread" 

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.