TThread
[Properties (by Name)][Methods (by Name)][Events (by Name)]
Abstract Thread class.
Declaration
Source position: classesh.inc line 1860
Type
TThread = class
private
PThreadQueueEntry = ^TThreadQueueEntry;
TThreadQueueEntry = record
Method : TThreadMethod;
Thread : TThread;
ThreadID : TThreadID;
Exception : TObject;
SyncEvent : PRTLEvent;
Next : PThreadQueueEntry;
end
;
public
TSystemTimes = record
IdleTime : QWord;
UserTime : QWord;
KernelTime : QWord;
NiceTime : QWord;
end
;
private
FProcessorCount : LongWord;
FHandle : TThreadID;
FTerminated : Boolean;
FFreeOnTerminate : Boolean;
FFinished : Boolean;
FSuspended : LongBool;
FReturnValue : Integer;
FOnTerminate : TNotifyEvent;
FFatalException : TObject;
FExternalThread : Boolean;
FSynchronizeEntry : PThreadQueueEntry;
class function GetCurrentThread : TThread; Static;
class function GetIsSingleProcessor : Boolean; Static;
class procedure InternalQueue(aThread: TThread; aMethod: TThreadMethod;
aQueueIfMain: Boolean); Static;
procedure CallOnTerminate;
function GetPriority : TThreadPriority;
procedure SetPriority(Value: TThreadPriority);
procedure SetSuspended(Value: Boolean);
function GetSuspended : Boolean;
procedure InitSynchronizeEvent;
procedure DoneSynchronizeEvent;
procedure SysCreate(CreateSuspended: Boolean; const StackSize: SizeUInt);
procedure SysDestroy;
protected
FThreadID : TThreadID;
procedure DoTerminate; Virtual;
procedure TerminatedSet; Virtual;
procedure Execute; Virtual; Abstract;
procedure Synchronize(AMethod: TThreadMethod);
class procedure Synchronize(AThread: TThread; AMethod: TThreadMethod);
procedure Queue(aMethod: TThreadMethod);
class procedure Queue(aThread: TThread; aMethod: TThreadMethod); Static;
procedure ForceQueue(aMethod: TThreadMethod);
class procedure ForceQueue(aThread: TThread; aMethod: TThreadMethod)
; Static;
ReturnValue : Integer;
Terminated : Boolean;
private
FSuspendEvent : PRTLEvent;
FInitialSuspended : Boolean;
FSuspendedInternal : longbool;
FThreadReaped : Boolean;
public
constructor Create(CreateSuspended: Boolean; const StackSize: SizeUInt);
destructor Destroy; Override;
class function CreateAnonymousThread(aProc: TProcedure) : TThread
; Static;
class procedure NameThreadForDebugging(aThreadName: UnicodeString;
aThreadID: TThreadID); Static;
class procedure NameThreadForDebugging(aThreadName: AnsiString;
aThreadID: TThreadID); Static;
class procedure SetReturnValue(aValue: Integer); Static;
class function CheckTerminated : Boolean; Static;
class procedure RemoveQueuedEvents(aThread: TThread;
aMethod: TThreadMethod); Static;
class procedure RemoveQueuedEvents(aMethod: TThreadMethod); Static;
class procedure RemoveQueuedEvents(aThread: TThread); Static;
class procedure SpinWait(aIterations: LongWord); Static;
class procedure Sleep(aMilliseconds: Cardinal); Static;
class procedure Yield; Static;
class procedure GetSystemTimes(out aSystemTimes: TSystemTimes); Static;
class function GetTickCount : LongWord; Static;
class function GetTickCount64 : QWord; Static;
class function ExecuteInThread(AMethod: TThreadExecuteHandler;
AOnTerminate: TNotifyEvent) : TThread
; Overload; Static;
class function ExecuteInThread(AMethod: TThreadExecuteStatusHandler;
AOnStatus: TThreadStatusNotifyEvent;
AOnTerminate: TNotifyEvent) : TThread
; Overload; Static;
class function ExecuteInThread(AMethod: TThreadExecuteCallBack;
AData: Pointer;
AOnTerminate: TNotifyCallBack) : TThread
; Overload; Static;
class function ExecuteInThread(AMethod: TThreadExecuteStatusCallBack;
AOnStatus: TThreadStatusNotifyCallBack;
AData: Pointer;
AOnTerminate: TNotifyCallBack) : TThread
; Overload; Static;
procedure AfterConstruction; Override;
procedure Start;
procedure Resume;
procedure Suspend;
procedure Terminate;
function WaitFor : Integer;
CurrentThread : TThread;
ProcessorCount : LongWord;
IsSingleProcessor : Boolean;
FreeOnTerminate : Boolean;
Handle : TThreadID;
ExternalThread : Boolean;
Priority : TThreadPriority;
Suspended : Boolean;
Finished : Boolean;
ThreadID : TThreadID;
OnTerminate : TNotifyEvent;
FatalException : TObject;
end
;
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.
Members
| Member | Type | Visibility | Description |
|---|---|---|---|
| AfterConstruction | Method | public | Code to be executed after construction but before execute. |
| CallOnTerminate | Method | private | |
| CheckTerminated | Method | public | Check if the current thread has finished executing. |
| Create | Method | public | Creates a new thread. |
| CreateAnonymousThread | Method | public | Execute code in an anonymous thread |
| CurrentThread | Property | public | Return current thread instance |
| Destroy | Method | public | Destroys the thread object. |
| DoneSynchronizeEvent | Method | private | |
| DoTerminate | Method | protected | Terminates the thread. |
| Execute | Method | protected | Execute method. Must be overridden in a descendant thread. |
| ExecuteInThread | Method | public | Execute a method or static procedure in a thread |
| ExternalThread | Property | public | Is the thread instance an external thread ? |
| FatalException | Property | public | Exception that occurred during thread execution |
| FExternalThread | Field | private | |
| FFatalException | Field | private | |
| FFinished | Field | private | |
| FFreeOnTerminate | Field | private | |
| FHandle | Field | private | |
| Finished | Property | public | Has the thread finished executing |
| FInitialSuspended | Field | private | |
| FOnTerminate | Field | private | |
| ForceQueue | Method | protected | |
| FProcessorCount | Field | private | |
| FreeOnTerminate | Property | public | Indicates whether the thread should free itself when it stops executing. |
| FReturnValue | Field | private | |
| FSuspended | Field | private | |
| FSuspendedInternal | Field | private | |
| FSuspendEvent | Field | private | |
| FSynchronizeEntry | Field | private | |
| FTerminated | Field | private | |
| FThreadID | Field | protected | |
| FThreadReaped | Field | private | |
| GetCurrentThread | Method | private | |
| GetIsSingleProcessor | Method | private | |
| GetPriority | Method | private | |
| GetSuspended | Method | private | |
| GetSystemTimes | Method | public | Return CPU stats |
| GetTickCount | Method | public | Return tick count (32-bit) |
| GetTickCount64 | Method | public | Return tick count (64-bit) |
| Handle | Property | public | Returns the thread handle. |
| InitSynchronizeEvent | Method | private | |
| InternalQueue | Method | private | |
| IsSingleProcessor | Property | public | Is the current system single processor or not |
| NameThreadForDebugging | Method | public | Set a thread name |
| OnTerminate | Property | public | Event called when the thread terminates. |
| Priority | Property | public | Returns the thread priority. |
| ProcessorCount | Property | public | Return the processor count for this system |
| PThreadQueueEntry | Type | private | |
| Queue | Method | protected | Queue a method for execution in the main thread |
| RemoveQueuedEvents | Method | public | Remove methods scheduled for execution from queue |
| Resume | Method | public | Resumes the thread's execution. Deprecated, see TThread.Start |
| ReturnValue | Property | protected | Return value of the thread when it stops executing. |
| SetPriority | Method | private | |
| SetReturnValue | Method | public | Set return value of a thread |
| SetSuspended | Method | private | |
| Sleep | Method | public | Prevent thread execution |
| SpinWait | Method | public | Prevent thread execution in a spin-wait loop |
| Start | Method | public | Starts a thread that was created in a suspended state. |
| Suspend | Method | public | Suspends the thread's execution. |
| Suspended | Property | public | Indicates whether the thread is suspended. |
| Synchronize | Method | protected | Synchronizes the thread by executing the method in the main thread. |
| SysCreate | Method | private | |
| SysDestroy | Method | private | |
| Terminate | Method | public | Signals the thread it should terminate. |
| Terminated | Property | protected | Indicates whether the Terminate method was called by the user. |
| TerminatedSet | Method | protected | |
| ThreadID | Property | public | Returns the thread ID. |
| TSystemTimes | Type | public | Record for returning processor usage |
| TThreadQueueEntry | Type | private | |
| WaitFor | Method | public | Waits for the thread to terminate and returns the exit status. |
| Yield | Method | public | Yield execution to other threads |
Inheritance
| Class | Description |
|---|---|
| TThread | Abstract Thread class. |
See also
| Name | Description |
|---|---|
| EThread | Thread error exception. |
| Integer | A signed 16-bits integer |
| Integer | A signed 16-bits integer |
| LongWord | An unsigned 32-bits integer |
| LongWord | An unsigned 32-bits integer |
| PRTLEvent | Pointer to RTLEvent, which is an opaque type. |
| TNotifyEvent | Standard event handler type. |
| TNotifyEvent | Standard event handler type. |
| TObject | Base class of all classes. |
| TObject | Base class of all classes. |
| TThread | Abstract Thread class. |
| TThread.Execute | Execute method. Must be overridden in a descendant thread. |
| TThread.ExecuteInThread | Execute a method or static procedure in a thread |
| TThreadID | Type for Thread-IDs |
| TThreadID | Type for Thread-IDs |
| TThreadID | Type for Thread-IDs |
| TThreadID | Type for Thread-IDs |
| TThreadPriority | Enumeration specifying the priority at which a thread runs. |