Delphi线程简介和它的create方法

TThread在Classes单元里的声明如下

type

    TThread = class

    private

        FHandle: THandle;

        FThreadID: THandle;

        FTerminated: Boolean;

        FSuspended: Boolean;

        FFreeOnTerminate: Boolean;

        FFinished: Boolean;

        FReturnValue: Integer;

        FOnterminate: TNotifyEvent;

        FMethod: TThreadMethod;

        FSynchronizeException: TObject;

        procedure CallOnTerminate;

        function GetPriority: TThreadPriority;

        procedure SetPriority(Value: TThreadPriority);

        procedure SetSuspended(value: Boolean);

    protected

        procedure DoTerminate; virtual;

        procedure Execute; virtual; abstract;

        procedure Synchronize(Method: TThreadMethod);

        property ReturnValue: Integer read FReturnValue write FReturnValue;

        property Terminated: Boolean read FTerminated;

    public 

        constructor Create(CreateSuspended: Boolean);

        destructor Destroy; override;

        procedure Resume;

        procedure Terminate;

        function WaitFor: Integer;

        property FreeOnTerminate: Boolean read FFreeOnTerminate write FFreeOnTerminate;

        property Handle: THandle read FHandle;

        property Priority: TThreadPriority read GetPriority write SetPriority;

        property Suspended: Boolean read FSuspended write Suspended;

        property ThreadID: THandle read FThreadID;

        property OnTerminate: TNotifyEvent read FOnTerminate write FOnTerminate;

    end;

  

  当TThread的Create()被调用的时候,需要传递一个布尔型的参数CreateSuspended。如果把这个参数设为False,那么当调用Create()之后,Execute()会被自动调用,也就是自动地执行线程代码。如果该参数设为True,则需要运行TThread的Resume()来唤醒线程。

  一般情况下,当你调用Create()后,还会有一些其他的属性要求设置。所以,应当把CreateSuspended参数设置为True,因为在TThread已经执行的情况下设置TThread的属性可能引起麻烦。

  在讲的深一点,在构造函数Create()中隐含调用一个RTL例程BeginThread(),而它又调用了一个API函数CreateThread()来创建一个线程对象的实例。CreateSuspended参数表明是否传递CREATE_SUSPENDED标志给CreateThread()

你可能感兴趣的:(Delphi)