type
TForm1 = class(TForm)
Button1: TButton;
Button3: TButton;
ListBox1: TListBox;
Button2: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TListThread = class(TThread)
protected
procedure Execute; override;
end;
TMyThread = class(TThread)
protected
procedure Execute; override;
end;
TYouThread = class(TThread)
protected
procedure Execute; override;
end;

var
Form1: TForm1;
threadList1: TThreadList;
mythreadRunning, youthreadRunning, listThreadRunning: Boolean;
globalCount: Integer;
listProcess: TListThread; { TListThread is a custom descendant of TThread. }
secondProcess: TMyThread; { TMyThread is a custom descendant of TThread. }
otherSecondProcess: TYouThread; { TMyThread is a custom descendant of TThread. }

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
if (mythreadRunning = FALSE) then
begin
mythreadRunning:= TRUE;
secondProcess := TMyThread.Create(True); { Create suspended--secondProcess does not run yet. }
secondProcess.FreeOnTerminate := True; { You do not need to clean up after termination. }
secondProcess.Priority := tpLower; // Set the priority to lower than normal.
secondProcess.Resume; { Now run the thread. }
end
else
MessageDlg('This thread is still running. You are going to hurt yourself!',
mtInformation, [mbOk], 0);
end;

procedure TMyThread.Execute;
var
I: Integer;
myRadio: TRadioButton;
begin
for I := 0 to 20 do
begin
if (Terminated) then
begin
mythreadRunning:= FALSE;
exit;
end;
myRadio:= TRadioButton.Create(Form1);
globalCount:= globalCount + 1;
myRadio.Name:= 'RadioButton' + IntToStr(globalCount);
threadList1.Add(myRadio);
Sleep(1000);
end;
mythreadRunning:= FALSE;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if (listthreadRunning = FALSE) then
begin
listThreadRunning:= TRUE;
listProcess := TListThread.Create(True); { Create suspended--secondProcess does not run yet. }
listProcess.FreeOnTerminate := True; { You do not need to clean up after termination. }
listProcess.Priority := tpLower; // Set the priority to lower than normal.
listProcess.Resume; { Now run the thread. }
end;
end;

procedure TListThread.Execute;
var
I: Integer;
Temp: TControl;
myList: TList;
begin
while(True) do
begin
if (Terminated) then
begin
listthreadRunning:= FALSE;
exit;
end;
Form1.ListBox1.Clear;
myList:= threadList1.LockList;
try
for I := 0 to myList.Count-1 do
begin
Temp:= myList.Items[I];
Form1.ListBox1.Items.Add(Temp.Name);
end;
finally
threadList1.UnlockList;
end;
Sleep(1000);
end;
listthreadRunning:= FALSE;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
if (youthreadRunning = FALSE) then
begin
youthreadRunning:= TRUE;
otherSecondProcess := TYouThread.Create(True); { Create suspended--secondProcess does not run yet. }
otherSecondProcess.FreeOnTerminate := True; { You do not need to clean up after termination. }
otherSecondProcess.Priority := tpLower; // Set the priority to lower than normal.
otherSecondProcess.Resume; { Now run the thread. }
end
else
MessageDlg('This thread is still running. You are going to hurt yourself!',
mtInformation, [mbOk], 0);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
if (listProcess <> nil) then
listProcess.Terminate;
if (secondProcess <> nil) then
secondProcess.Terminate;
if (otherSecondProcess <> nil) then
otherSecondProcess.Terminate;
end;

procedure TYouThread.Execute;
var
I: Integer;
Temp: TControl;
begin
for I := 0 to 10 do
begin
if (Terminated) then
begin
youThreadRunning:= FALSE;
exit;
end;
with threadList1.LockList do
try
if (2*I < Count) then
begin
Temp:= Items[2*I];
threadList1.Remove(Temp);
end;
finally
threadList1.UnlockList;
end;
if (Terminated) then
MessageDlg('youThread has been asked to terminate, but is still running!',
mtInformation, [mbOk], 0);
Sleep(3000);
end;
youthreadRunning:= FALSE;
end;

http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/TThreadList_(Delphi)

TThreadList Demo的更多相关文章

  1. 通过一个demo了解Redux

    TodoList小demo 效果展示 项目地址 (单向)数据流 数据流是我们的行为与响应的抽象:使用数据流能帮我们明确了行为对应的响应,这和react的状态可预测的思想是不谋而合的. 常见的数据流框架 ...

  2. 很多人很想知道怎么扫一扫二维码就能打开网站,就能添加联系人,就能链接wifi,今天说下这些格式,明天做个demo

    有些功能部分手机不能使用,网站,通讯录,wifi基本上每个手机都可以使用. 在看之前你可以扫一扫下面几个二维码先看看效果: 1.二维码生成 网址 (URL) 包含网址的 二维码生成 是大家平时最常接触 ...

  3. 在线浏览PDF之PDF.JS (附demo)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#skill 下载地址:http://mozilla.gith ...

  4. 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo

    Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...

  5. vue双向数据绑定原理探究(附demo)

    昨天被导师叫去研究了一下vue的双向数据绑定原理...本来以为原理的东西都非常高深,没想到vue的双向绑定真的很好理解啊...自己动手写了一个. 传送门 双向绑定的思想 双向数据绑定的思想就是数据层与 ...

  6. Android Studio-—使用OpenCV的配置方法和demo以及开发过程中遇到的问题解决

    前提: 1.安装Android Studio(过程略) 2.官网下载OpenCV for Android 网址:http:opencv.org/downloads.html 我下载的是下图的版本 3. ...

  7. iOS之ProtocolBuffer搭建和示例demo

    这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...

  8. 钉钉开放平台demo调试异常问题解决:hostname in certificate didn't match

    今天研究钉钉的开放平台,结果一个demo整了半天,这帮助系统写的也很难懂.遇到两个问题: 1.首先是执行demo时报unable to find valid certification path to ...

  9. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

随机推荐

  1. 百度echart--Uncaught Error: Component series.wordCloud not exists. Load it first.

    百度echart--Uncaught Error: Component series.wordCloud not exists. Load it first. 一.总结 一句话总结:关注报的错.可以通 ...

  2. [Ramda] Get a List of Unique Values From Nested Arrays with Ramda (flatMap --> Chain)

    In this lesson, we'll grab arrays of values from other arrays, resulting in a nested array. From the ...

  3. 浅谈 Redis 与 MySQL 的耦合性以及利用管道完成 MySQL 到 Redis 的高效迁移

    http://blog.csdn.net/dba_waterbin/article/details/8996872 ㈠ Redis 与 MySQL 的耦合性            在业务架构早期.我们 ...

  4. hibernate基本配置与简单增删改查

    ORM(Object Relation Mapping)是对象关系映射,是一个思想,它的作用是在关系数据库与对象之间做一个自动映射,将数据库中的表格映射到一个类,也就是持久化类,数据表中每行映射为对象 ...

  5. Android菜鸟的成长笔记(22)——Android进程间传递复杂数据(AIDL)

    在上一篇中介绍了Andorid中的进程间的通信方式AIDL,本篇文章将介绍传递复杂数据的AIDL Service 下面通过一个示例说明: 本例子中用到了两个自定义类型:Person与Pet, 其中Pe ...

  6. FileReader的编码问题

    有一个UTF-8编码的文本文件,用FileReader读取到一个字符串,然后转换字符集:str=new String(str.getBytes(),"UTF-8");结果大部分中文 ...

  7. 详解Qt,并举例说明动态编译(shared)和静态编译(static)以及debug and release 编译版本区别(可产生静态版的Debug版本,需要把-release 改为 –debug-and-release)

    作为初入Qt学习的新人,花了整整一两天时间,对Qt编译版本等问题进行了一步步探索,首先感谢网站博客中文章,开始也不是很明白一些几个问题: 1.Qt版本问题 作为初学者,可能下载时这么多版本,如何选择呢 ...

  8. 树莓派的rc.local档(设置开机)

    为了树莓派执行命令或程序时启动.需要被添加到顺序rc.local档.这是为那些谁执行后,直接要权力树莓派没有配置.或者不希望每次都手动启动该程序很实用. 的方法是使用cron和crontab. EDI ...

  9. webpack优化经验1(持续)

    1 不知道该优化哪里 先开启gzip压缩,这样可以很直接的减少请求包的体积,效果显著,不过需要在服务器端作相应的配置才能生效 2拆分vendor包, 减少单体包的体积,并行加载 通过配置,将不同的公用 ...

  10. 3.RabbitMQ相关概念的杂谈

    1.vhost,为什么我要有这个vhost呢? 这是因为可能有很多组使用RabbitMQ,有产品组,用户组,vhost,虚拟主机的意思,可以避免命名冲突. 2.Exchange,交换机 有四种交换机 ...