TTHREAD - Thread class detailed

xiaoxiao2021-03-06  66

TTHREAD is an abstract class that can create several independent threads. Class relationship TOBJECT creates a TTHREAD in a multi-threaded application represents a thread. An example of a TTHREAD object for each new subclass is a new thread. Multi-threaded instances derived from TTHREAD can form a multi-threaded application of Delphi. When an application is running, the application is loaded into memory preparation execution. At this point, it becomes a process containing one or more threads, each thread contains data, code, and system resources. The thread performs some of the contents of the application and assigns CPU time by the system. All threads of the same process share the same address space and access the global variable of the process. The thread improves the performance of the application by the following work: Manage the input of multi-communication devices. The priority of the task is distinguished. High priority processing urgent tasks. The priority is low. Other tasks. The following is some suggestions using threads: Turn too much thread consumption CPU time. For a single processor system, one process has up to 16 threads. When multiple threads are updated the same resource, threads should be synchronized to avoid conflicts. Methods Most access to VCL objects and update forms must be called from the main VCL thread. The following is a process of creating and using a new thread: (1) Click the File | New | Thread menu item, create a new unit containing an object, which is derived from the TTHREAD class. (2) Define the new thread object and the CREATE method. (3) Define the thread object and the Execute method by the code required to execute the thread. (4) Pass any call to the VCL component to the Synchronize method to avoid multithreaded conflicts.

Attribute list FreeOnterminate thread Termination When the object is automatically deleted, the Handle contains the thread handle Priority to determine the priority ReturnValue of the thread relative to other threads in the process Returns the consispended indicating whether the thread is suspended Terminated indicates that the thread is required to terminate the ThreadID identity throughout the system the rout

Method List ~ TTHREAD Deletes thread objects and releases its war's memory space doteerminate generating an onterminate event EXECUTE to provide a method of abstract way to include thread execution, resume re-execute a hangs Suspend Snone a running thread Synchronize The main VCL thread is executed to set the termination attribute to the True Notification Thread TTHREAD to create a thread object Waitfor WWITFOR Waitfor WWIT WATFOR Waiting for Thread Termination and Returns ReturnValue Attribute Values

Event list The ontermneticExecute method has returned and the thread is removed before being deleted.

Attributes

TTHREAD:: FreeOnterminate__property bool freeonterminate = {read = ffreeonterminate, write = ffreeonterminate, nodefault}; determination When the thread is terminated, the thread object is automatically deleted. FreeOnterminate default value is false, thread objects must be displayed in the code. Contains a thread handle. When calling the Win32API function to handle threads, use Handle.

Tthread :: priority__property tthreadpriority priority = {read = getPriority, Write = setPriority, NodeFault}; determine the priority of the thread relative to other threads in the process. The Priority property is a type of type, which defaults to the TPNORMAL.THREADPRIORITY type defines the possible values ​​of the Priority property of the TTHREAD component, as described in the table below. Windows determines the CPU cycle of each thread based on priority. Value Meaning _____________________________________________________________________tpIdle _____________________________________________________________________ tpLowest the thread execution thread priority lower than the normal 2 points tpLower thread priority lower than the normal 1:00 tpNormal normal priority thread tpHigher 1:00 tpHighest thread priority thread priority higher than normal only when the system is idle Level ratio of normal high 2 points TPTIMECRITICAL thread priority TTHREAD :: ReturnValue__property int ReturnValue = {read = FRETURNVALUE, WRITE = FRETURNVALUE, NODEFAULT}; Returns thread value. Use the RETURNVALUE to indicate its success / fail or digital results / output to other threads. The WaitFor method returns the value stored in ReturnValue.

TTHREAD :: suspended__property bool suspended = {read = fsuspended, write = setsuspended, nodefault}; indicating whether a thread is hang. Unless re-executed, the hanging thread will not continue. If you will be set to True, you will hang a thread; if you set it to false, continue the thread.

TTHREAD:: TERMINATED__PROPERTY BOOL TERMINATED = {read = fterminated, nodefault}; indicating that the thread is required to terminate. The Terminate method sets the Terminated property to True. The thread's Execute method and any Execute call method will periodically check Terminated, when it is True, will be terminated.

Tthread :: threadid__property int threadid = {read = fHreadId, nodefault}; identifies threads throughout the system. THREADID will be very useful when calling the Win32API function to handle threads. Note: ThreadID is different from the Handle property.

method

TTHREAD :: ~ TTHREAD__FASTCALL VIRVUAL ~ TTHREAD (VOID); Remove thread objects and releases its overcome memory space. Do not call ~ TTHREAD in your application. Use delete. ~ TTHREAD Notification Thread Termination and Wait for the thread to return before calling the Destroy method.

TTHREAD :: DoterminateVirtual void __fastcall doteerminate (void); generate an onterminate event. Doterminate calls the ONTERMINATE time handle but does not terminate the thread.

TTHREAD :: EXECUTEVIRTUAL VOID __FASTCALL EXECUTE (VOID) = 0; provides an abstraction method that contains the code required for thread execution. Execute View the Terminated property value to determine if the thread needs to be terminated. When CreateSuspended is set to false, when the CREATE is called, a thread is executed; the resume is called after the thread is created, and createSuspend is TRUE, a thread execution. Note: Do not call the properties and methods of other objects directly in the EXECUTE method of the thread. The use of other objects should be divided into several different processes, which are called as a parameter passed to the SYNCHRONIZE method. TTHREAD :: Resumevoid __fastcall resume (void); re-execute a hang-up thread. Call Suspend can be nested. Therefore, calling Resume must pay attention to the order.

TTHREAD :: Suspendvoid __fastcall suspend (void); hangs a running thread. Calling Resume can continue to run. Call Suspend can be nested. So call the resume must order.

TTHREAD :: SynchroniztyPedef void __fastcall (__closure * tthreadMethod) (void); void __fastcall synchronize (TTHREADMETHOD & METHOD); executes Method in the primary VCL thread. The SYNCHRONIZE method is executed by the call specified by Method by the primary VCL thread. Note: When Method is executed in the primary VCL thread, the current thread is hang.

TTHREAD :: Terminatevoid __fastcall terminate (void); By setting the TERMINATED property to the True Notification thread termination. The thread's Execute method and any method of Execute call should be periodically check Terminated when it is terminated when it is True.

TTHREAD :: Tthread__fastCall Tthread (Bool createSuspend); Create an instance of a thread object. Do not use TTHREAD directly in your application to create threads. Use new replacement to pass the CreateSuspended parameter argument. If CreateSuspend is false, Execute is called immediately. If CreateSuspended is True, Execute is called until the resume is called.

TTHREAD :: Waitforint __fastcall waitfor (void); Waiting for the thread to terminate and returns the returnval property value. The Waitfor returns until the thread is terminated, so the thread must be due to ending the Execute method or because the Terminated property is terminated for TRUE. If the thread uses Synchronize, do not call the Waitfor in the primary VCL thread, otherwise or cause the system crash, or generate an Ethread exception. Synchronize is allowed to wait for the primary VCL thread to enter the information loop before allowing the method to take effect. If the primary VCL thread has called Waitfor, it will not enter the information circuit, and SYNCHRONIZE will never return. At this point, TTHREAD will generate an ETHREAD accident and terminate the thread; and if the accident is not intercepted by the Execute method, the application will also terminate. If you call WaitFor, Synchronize is already waiting in the VCL thread, TTHREAD will not intervene, the application will crash.

event

转载请注明原文地址:https://www.9cbs.com/read-109824.html

New Post(0)