Indy 9
|
TIdThread = class(TIdBaseThread)
TIdThread extends the functionality of TThread to include flexible methods for control of thread state and notification of changes to thread state.
TIdThread implements the inherited abstract Execute method to provide a known thread execution mechanism. Execute can detect thread termination, suspend and resume threads, and provide exception handling for an exception raised during thread execution. Execute also provides finer control of thread execution by extending TThread to include the equivalent of thread event handlers.
TIdThread and descendants should override the abstract Run method, and implement the virtual methods BeforeRun and AfterRun.
The Execute method iterates through a loop, making calls to BeforeRun, Run, and AfterRun to provide thread functionality. This loop can be stopped by calling the Stop method. Use the Start method to begin or resume execution of the loop.
TIdThread descendants can be used in conjunction with a TIdThreadMgr descendants to address the overhead issues associated with thread creation and resource allocation in a multi-threaded application.
Note: Do not use properties and methods of other objects directly in the Run method of a thread. Use the Synchronize method to call a procedure that can access objects and resources which are not thread-safe.
property Data: TObject;
Note: It is the responsibility of the TIdThread descendant to cast Data to the class instance required by the TIdThread descendant, or an exception will be raised.
Data is not initialized in the Create constructor, but is released in the Destroy destructor.
property StopMode: TIdThreadStopMode;
property Stopped: Boolean;
Note that a thread may still be executing even if its Stopped property is True.
property TerminatingException: string;
property TerminatingExceptionClass: TClass;
constructor Create(ACreateSuspended: Boolean = True); virtual;
Create insures that GNotifyThread contains a valid TIdNotifyThread instance prior to extiing from the method.
destructor Destroy; override;
procedure Start; virtual;
procedure Stop; virtual;
When the Stop method is called, it immediately returns, but the thread may have to wait for completion of thread of execution before it can be suspended or terminated. In contrast, if the TerminateAndWaitFor method is called instead, it returns only after the thread has left the main thread of execution.
If you want to immediately suspend the thread, you should set Suspended to true.
procedure Terminate; virtual;
Terminate will be called in Execute when an exception is raised during execution of the thread. Terminate is also called when Stop is executed and StopMode contains smTerminate.
procedure TerminateAndWaitFor; virtual;
property OnException: TIdExceptionThreadEvent;
Note: Do not free the thread object within this event handler. Allow the OnException event handler to return to the thread context for proper thread termination.
The text portion of the Exception message can also be found in TerminatingException.
property OnStopped: TIdNotifyThreadEvent;
Applications must assign a procedure to the event handler to allow a response to the event notification.