Delphi Thread类的创建及使用(关于线程函数的传递例子)


本文整理自网络,侵删。

 Delphi Thread类的创建及使用

一、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.

相关阅读 >>

Delphi 对话框单元

Delphi编写提取exe文件的ico图标

Delphi在richedit控件中插入gif动画表情

Delphi在combobox下拉框里显示图片

Delphi 计算文件大小

Delphi 把窗体客户区图像保存到文件或剪切板

Delphi 如何用程序控制禁止和起用上网?

Delphi 安卓app的http访问权限

Delphi sha1加密函数

Delphi 将svg加载到timage控件

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...