delphi一个简单的多线程例子


本文整理自网络,侵删。

 我的这个示例实为一个主线程,两个辅线程,辅线程是重新定义了两线程类,Tthread1和Tthread2,都是从Tthread继承而来。

关键是两个辅线程的Execute事件重载了祖先类Tthread的Execute.实现了相应的功能。这两个线程的事件可同时执行,无需等待。

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
Tthread1 = class(Tthread)
protected
procedure Execute;override;
end;
type
Tthread2 = class(Tthread)
protected
procedure Execute;override;
end;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
st1: TStaticText;
st2: TStaticText;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
procedure Tthread1.Execute;
var
i: integer;
begin
FreeOnTerminate:=true;
for i:=1 to 10000 do
begin
form1.st1.Caption:= inttostr(i);
application.ProcessMessages;
end;
end;

procedure Tthread2.Execute;
var
i: integer;
begin
FreeOnTerminate:=true;
for i:=1 to 10000 do
begin
form1.st2.Caption:= inttostr(i);
application.ProcessMessages;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
mythread:Tthread1;
begin
mythread:=Tthread1.Create(false);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
mythread:Tthread2;
begin
mythread:=Tthread2.Create(false);
end;

end.

相关阅读 >>

Delphi 创建文件夹并打开

Delphi 处理之文本文件

Delphi 获取安卓手机wifi信息(xe8)

Delphi改变文件夹图标

Delphi获取系统字体列表

Delphi goto语句用法

Delphi 提取字符串左侧内容

idhttp使用时内存猛增,如何解决

Delphi 判断目录是否存在,不存在则创建目录并打开,存在则直接打开

Delphi 程序窗体最大化、组件居中显示代码

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



打赏

取消

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

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

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

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

评论

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