Delphi中使用TThread类实现多线程
之前写的多线程程序都是用api直接做的,没有用过delphi自带Tthread类,刚才看了下,写了个demo,希望对新手有帮助。。。
窗体单元:
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,myThread, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure TThreadFinsh(Sender:TObject);
public
{ Public declarations }
end;
var
Form1: TForm1;
t1,t2,t3:TThread;
implementation
{$R *.dfm}
procedure TForm1.TThreadFinsh(Sender:TObject);
begin
ShowMessage('一个线程完毕!');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Button1.Caption='开始1' then
begin
Button1.Caption:='关闭';
t1.Resume;
end
else
begin
Button1.Caption:='开始1';
t1.Suspend;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if Button2.Caption='开始2' then
begin
Button2.Caption:='关闭';
t2.Resume;
end
else
begin
Button2.Caption:='开始2';
t2.Suspend;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if Button3.Caption='开始3' then
begin
Button3.Caption:='关闭';
t3.Resume;
end
else
begin
Button3.Caption:='开始3';
t3.Suspend;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
t1:=TmyThread1.Create(Label1,10);
t1.OnTerminate:=TThreadFinsh;
t2:=TmyThread2.Create(Label2,20);
t2.OnTerminate:=TThreadFinsh;
t3:=TmyThread3.Create(Label3,30);
t3.OnTerminate:=TThreadFinsh;
end;
end.
线程单元:
unit myThread;
interface
uses
Classes,Windows,SysUtils,Forms,StdCtrls;
type
TTestThread = class(TThread)
private
FLabel:TLabel;
FSleepDec:Integer;
protected
procedure Execute; override;
public
constructor Create(lbl:TLabel;sleepSec:Integer);
end;
TmyThread1=class(TTestThread) end;
TmyThread2=class(TTestThread) end;
TmyThread3=class(TTestThread) end;
implementation
uses main;
{ TTestThread }
constructor TTestThread.Create(lbl:TLabel;sleepSec:Integer); //参数传递
begin
FLabel:=lbl;
FSleepDec:=sleepSec;
FreeOnTerminate:=True; //让线程终止是触发OnTerminate事件
inherited Create(True);//不立即执行,只有调用resume才开始
end;
procedure TTestThread.Execute;
var
i:Integer;
begin
for i:=0 to 1000 do
begin
if terminated then Break;
FLabel.Caption:=IntToStr(i);
Sleep(FSleepDec);
end;
end;
end.
Delphi中使用TThread类实现多线程相关推荐
- delphi android 多线程,Delphi中使用TThread进行多线程开发总结
最近项目的Demo开发使用Delphi 2010,其中使用了多线程开发技术 这里采用VCL的TThread类完成多线程开发,直接使用CreateThread的办法之前用过很多,这里就当练习,试试采用T ...
- Delphi中的线程类--之(1)
Delphi中的线程类 猛禽[Mental Studio] http://mental.mentsu.com ( 之一) Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大多数D ...
- Delphi中的线程类--之(2)
Delphi中的线程类 猛禽[Mental Studio] http://mental.mentsu.com 之二 首先就是构造函数: constructor TThread.Create(Creat ...
- DELPHI 中的自定义类笔记(一) OBJECT 创建类 不用 CREATE 【转】
原文:http://www.cnblogs.com/delphi-xe5/articles/3884154.html Delphi 中的自定义类笔记(一) 类的声明 有两种声明办法 一.自定义类的父类 ...
- Delphi中的线程类
Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大多数Delphi书藉都有说到,但基本上都是对TThread类的几个成员作一简单介绍,再说明一下Execute的实现和Synchr ...
- Delphi中的线程类Thread
原文:http://www.heibai.net/article/info/info.php?infoid=22594 Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大多数De ...
- Delphi 中的 XMLDocument 类详解(5) - 获取元素内容
为什么80%的码农都做不了架构师?>>> unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Clas ...
- Delphi中的TStream类
什么是流? TStream? ( What Is a Stream? TStream? ) A stream is what its name suggests: a flowing "ri ...
- Delphi 中的 XMLDocument 类详解(13) - 关于 XML 属性
为什么80%的码农都做不了架构师?>>> unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Clas ...
最新文章
- OS study plan
- 我对CTO的理解 CTO要有技术魅力
- 对python的认识800字_我对python里True和False的理解
- mysql 图形化工具
- 【深度学习】重新思考BatchNorm中的 “Batch”
- 【转载】15分钟搭建Linux操作系统+Oracle数据库的环境
- Java反射基础(二)--Fileds对象的使用
- h5获取http请求头_nodejs 中http请求头,响应头
- api函数原型大全_如何使用您的API优先平台来使原型做好生产准备
- FLASH得到MYSQL数据
- 快准狠才叫爽!六款智能电视语音识别大比拼
- 安装时间大于30秒_高送转第一股秒板,封单金额近百亿!最新高送转潜力股名单曝光...
- arp计算机病毒解决办法,“ARP病毒的解决方案”的解决方案
- 51nod 1534棋子游戏(分析)
- 如何用C语言实现批量修改文件类型
- SSD202 移植 新屏幕
- vue集成svg-sprite-loader
- ERROR: node with name rabbit already running on xxx
- 计算机网络——路由算法
- Java面向对象知识点小结