【Delphi】多线程使用(1)
来源:互联网 发布:爱言叶 动作数据 编辑:程序博客网 时间:2024/06/10 04:10
整理万一博客 线程学习 每个例子 均运行和使用 运行环境(我自己使用的是xe4,其他版本也可 )(附源码)
什么是线程
VCL线程的使用
TThread类
属性
FreeOnTerminate属性(指定当线程终止时是否自动删除线程对象 默认:False(有的书上说是 true,建议使用时候,直接赋值))
Priority属性 设置线程的优先级
ReturnValue属性 当线程终止时,用该属性为其他线程传递一个返回值
Suspended属性,指明线程是否出于挂起状态
Terminated属性,线程运行时,置为true,则应将线程终止
ThreadID 线程ID
Handle 线程句柄
方法
DoTerminate方法-触发OnTerminate事件
Execute方法-创建线程必须实现的方法
Resume 当线程对象处于暂停状态时,调用此方法可以恢复线程运行
Suspend 当线程运行时,调用此方法可以暂停线程运行
Synchronize 避免多个线程同时操作一个对象导致程序崩溃
Terminate 终止线程
WaitFor 等待线程对象的终止,并返回线程对象的ReturnValue属性值
举例说明
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TForm1 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Form1: TForm1;
implementation
type
TMyThread = class(TThread)
protected
procedure Execute; override;
end;
{$R *.dfm}
type
TMyThread = class(TThread)
protected
procedure Execute; override;
end;
{$R *.dfm}
{ TMyThread }
procedure TMyThread.Execute;
var
i:Integer;
begin
inherited;
for i := 0 to 100000 do
begin
//这段代码还是加上
FreeOnTerminate := True;
with Form1.Canvas do
begin
Lock;
TextOut(10,10,IntToStr(i));
Unlock;
end;
end;
var
i:Integer;
begin
inherited;
for i := 0 to 100000 do
begin
//这段代码还是加上
FreeOnTerminate := True;
with Form1.Canvas do
begin
Lock;
TextOut(10,10,IntToStr(i));
Unlock;
end;
end;
end;
procedure TForm1.btn1Click(Sender: TObject);
begin
TMyThread.Create(False);
end;
begin
TMyThread.Create(False);
end;
end.
0 0
- 【Delphi】多线程使用(1)
- Delphi 多线程编程(1)
- Delphi中使用多线程
- delphi 2010多线程使用
- delphi——多线程(1)
- delphi多线程(2)
- delphi 多线程(一)
- Delphi 多线程编程(3)
- Delphi 多线程 (API篇)
- Delphi中使用TThread类实现多线程
- Delphi多线程和indy10 tcp的使用。
- delphi多线程
- delphi多线程
- delphi 多线程
- delphi多线程
- delphi多线程
- delphi 多线程
- Delphi多线程
- nodejs开发聊天室 存几个博客ql
- Storm DRPC环境搭建笔记
- javescript基础2(变量/数组)
- rhel crontab任务计划调度
- HDU 5135 Little Zu Chongzhi's Triangles(状压dp或者贪心)
- 【Delphi】多线程使用(1)
- 梦想是你的方向,是你的未来
- Struts2常用标签总结
- poj1639 Picnic Planning 限制顶点度数的MST
- set--STL
- nanoPC-T1 4412u-boot启动
- css3 animation动画对应属性解释
- Oracle - 系统权限详细列表
- 【Android 一般进阶】如何退出app