Skip to content

TThread

Abstract Thread class.

Declaration

Source position: classesh.inc line 2392

type

  TThread = class
  public
    TSystemTimes = record
      IdleTime: QWord;
      UserTime: QWord;
      KernelTime: QWord;
      NiceTime: QWord;
    end;
    constructor Create;
    destructor Destroy;
    class function CreateAnonymousThread(aProc: TProcedure) : TThread;
    class function CreateAnonymousThread(aProc: TThreadMethod) : TThread;
    class procedure NameThreadForDebugging(aThreadName: UnicodeString; 
                                      aThreadID: TThreadID=TThreadID(- 1));
    class procedure NameThreadForDebugging(aThreadName: AnsiString; 
                                      aThreadID: TThreadID=TThreadID(- 1));
    class procedure SetReturnValue;
    class function CheckTerminated : Boolean;
    class procedure RemoveQueuedEvents(aThread: TThread; 
                                  aMethod: TThreadMethod);
    class procedure RemoveQueuedEvents(aMethod: TThreadMethod);
    class procedure RemoveQueuedEvents(aThread: TThread);
    class procedure SpinWait;
    class procedure Sleep;
    class procedure Yield;
    class function GetSystemTimes : Boolean;
    class function GetCPUUsage : Integer;
    class function GetTickCount : LongWord;
    class function GetTickCount64 : QWord;
    class function ExecuteInThread(AMethod: TThreadExecuteHandler; 
                              AOnTerminate: TNotifyEvent=Nil) : TThread; overload;
    class function ExecuteInThread(AMethod: TThreadExecuteStatusHandler; 
                              AOnStatus: TThreadStatusNotifyEvent; 
                              AOnTerminate: TNotifyEvent=Nil) : TThread; overload;
    class function ExecuteInThread(AMethod: TThreadExecuteCallBack; 
                              AData: Pointer=Nil; 
                              AOnTerminate: TNotifyCallBack=Nil)
                               : TThread; overload;
    class function ExecuteInThread(AMethod: TThreadExecuteStatusCallBack; 
                              AOnStatus: TThreadStatusNotifyCallBack; 
                              AData: Pointer=Nil; 
                              AOnTerminate: TNotifyCallBack=Nil)
                               : TThread; overload;
    procedure AfterConstruction;
    procedure Start;
    procedure Resume;
    procedure Suspend;
    procedure Terminate;
    function WaitFor : Integer;
    class function CurrentIsMain : Boolean;
    class property CurrentThread: TThread; {r}
    class property Current: TThread; {r}
    class property ProcessorCount: LongWord; {r}
    class property IsSingleProcessor: Boolean; {r}
    property FreeOnTerminate: Boolean; {rw}
    property Handle: TThreadHandle; {r}
    property ExternalThread: Boolean; {r}
    property Priority: TThreadPriority; {rw}
    property Suspended: Boolean; {rw}
    property Finished: Boolean; {r}
    property ThreadID: TThreadID; {r}
    property OnTerminate: TNotifyEvent; {rw}
    property FatalException: TObject; {r}
  end;

Inheritance

Description

The TThread class encapsulates the native thread support of the operating system. To create a thread, declare a descendant of the TThread object and override the Execute method. In this method, the tthread's code should be executed. To run a thread, create an instance of the tthread descendant, and call it's execute method.

It is also possible to simply execute a method or static procedure in a thread using the TThread.ExecuteInThread class method.

Properties

Member Visibility Description
Current read only public
CurrentThread read only public Return current thread instance.
ExternalThread read only public Is the thread instance an external thread ?
FatalException read only public Exception that occurred during thread execution.
Finished read only public Has the thread finished executing.
FreeOnTerminate public Indicates whether the thread should free itself when it stops executing.
Handle read only public Returns the thread handle.
IsSingleProcessor read only public Is the current system single processor or not.
Priority public Returns the thread priority.
ProcessorCount read only public Return the processor count for this system.
Suspended public Indicates whether the thread is suspended.
ThreadID read only public Returns the thread ID.

Methods

Member Visibility Description
AfterConstruction public Code to be executed after construction but before execute.
CheckTerminated public Check if the current thread has finished executing.
Create public Creates a new thread.
CreateAnonymousThread public Execute code in an anonymous thread.
CurrentIsMain public
Destroy public Destroys the thread object.
Execute protected Execute method. Must be overridden in a descendant thread.
ExecuteInThread public Execute a method or static procedure in a thread.
ForceQueue protected Always queue a method for execution in the main thread.
GetCPUUsage public
GetSystemTimes public Return CPU stats.
GetTickCount public Return tick count (32-bit).
GetTickCount64 public Return tick count (64-bit).
NameThreadForDebugging public Set a thread name.
Queue protected Queue a method for execution in the main thread.
RemoveQueuedEvents public Remove methods scheduled for execution from queue.
Resume public Resumes the thread's execution. Deprecated, see TThread.Start.
SetReturnValue public Set return value of a thread.
Sleep public Prevent thread execution.
SpinWait public Prevent thread execution in a spin-wait loop.
Start public Starts a thread that was created in a suspended state.
Suspend public Suspends the thread's execution.
Synchronize protected Synchronizes the thread by executing the method in the main thread.
Terminate public Signals the thread it should terminate.
WaitFor public Waits for the thread to terminate and returns the exit status.
Yield public Yield execution to other threads.

Events

Member Visibility Description
OnTerminate public Event called when the thread terminates.

See also

Name Description
EThread Thread error exception.
TThread.Execute Execute method. Must be overridden in a descendant thread.
TThread.ExecuteInThread Execute a method or static procedure in a thread.