zoukankan      html  css  js  c++  java
  • Delphi线程简介---Create及其参数、Resume、Suspend

    转载自:https://www.cnblogs.com/xumenger/p/4440620.html

    TThread在Classes单元里的声明如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    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 

  • 相关阅读:
    Python3中字符串的编码与解码以及编码之间转换(decode、encode)
    Python 封装
    Python多态、鸭子类型
    Python抽象类和接口类
    Python 类的名称空间和组合
    Python面向对象
    Python 模块和包
    接口自动化之cookies/session
    jmeter之cookies登录
    jenkins发送邮件
  • 原文地址:https://www.cnblogs.com/approx/p/11852088.html
Copyright © 2011-2022 走看看