飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 3399|回复: 0

Thread类的创建及使用

[复制链接]
  • TA的每日心情
    开心
    2019-9-19 16:05
  • 签到天数: 4 天

    [LV.2]偶尔看看I

    发表于 2007-2-8 11:07:10 | 显示全部楼层 |阅读模式
    一、Thread类的创建:

    unit Thread;
    {             线程类的创建
      编译环境: Windows 2003 Sever  Delphi 7.0 Enterprise
    }
    interface
    uses classes,sysutils,StdCtrls;
    type
      TB = class(TThread)
      private
        i :integer;
        Fedt :TEdit;
        procedure Update ;
      public
        procedure execute;override;
        constructor create(IsSuspended :Boolean;edt :TEdit);
      end;
    implementation
    uses MainForm;

    procedure TB.Update;
    begin
      Fedt.Text :=inttostr(i);
    end;

    constructor TB.create(IsSuspended: Boolean; edt: TEdit);
    begin
      inherited create(IsSuspended);
      Fedt := edt;
    end;

    procedure TB.execute;
    begin
      i:=0;
      while(not Terminated) do
      begin
        Synchronize(Update);
        inc(i);
      end;
    end;
    end.

    二、Thread类的使用:

    unit MainForm;

    interface

    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls,Thread;

    type
      TfrmMain = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        procedure FormCreate(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure ButtonClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;


    var
      frmMain: TfrmMain;
      a,b:TB;
    implementation

    {$R *.dfm}

    procedure TfrmMain.FormCreate(Sender: TObject);
    begin
      a:=TB.create(true,edit1);
      b:=TB.create(True,edit2);
    end;

    procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      freeandnil(a);freeandnil(b);
    end;

    procedure TfrmMain.ButtonClick(Sender: TObject);
    var c :TB;
    begin
      if sender = Button1 then c :=a
      else c:=b;
      if c.Suspended then begin
        c.Resume ; (sender as TButton).Caption :='暂停';
      end else begin
        c.Suspend ;(Sender as TButton).Caption :='开始';
      end;
    end;

    end.
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表