转载自: https://www.cnblogs.com/xumenger/p/4440620.html
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 的参数
当 TThread 的 Create() 被调用的时候, 需要传递一个布尔型的参数 CreateSuspended. 如果把这个参数设为 False, 那么当调用 Create() 之后, Execute() 会被自动调用, 也就是自动地执行线程代码. 如果该参数设为 True, 这样创建了线程, 但是线程创建完成之后是挂起的, 需要调用 TThread 的 Resume() 来唤醒线程执行.
一般情况下, 当你调用 Create() 后, 还会有一些其他的属性要求设置. 所以, 这种情况下需要把 CreateSuspended 参数设置为 True, 因为在 TThread 已经执行的情况下设置 TThread 的属性可能引起麻烦.
同时注意 TThread 的 Create 方法是没有默认参数的, 所以你在创建的一个继承自 TThread 的线程类没有重写 Create 方法的话, 调用 Create 的时候必须传入 True 或 False. 不过如果你实现一个继承自 TThread 的类, 并且可以自己 override(重写)TThread 的 Create 方法, 这时候你当然就可以自己定制, 比如可以设计一个默认参数为 CreateSuspended 为 True 或 False.
在讲的深一点, 在构造函数 Create() 中隐含调用一个 RTL 例程 BeginThread(), 而它又调用了一个 API 函数 CreateThread() 来创建一个线程对象的实例. CreateSuspended 参数表明是否传递 CREATE_SUSPENDED 标志给 CreateThread().
再说一下 Suspend 和 Resume
来源: http://www.bubuko.com/infodetail-3286439.html