TThreadList Demo
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的更多相关文章
- 通过一个demo了解Redux
TodoList小demo 效果展示 项目地址 (单向)数据流 数据流是我们的行为与响应的抽象:使用数据流能帮我们明确了行为对应的响应,这和react的状态可预测的思想是不谋而合的. 常见的数据流框架 ...
- 很多人很想知道怎么扫一扫二维码就能打开网站,就能添加联系人,就能链接wifi,今天说下这些格式,明天做个demo
有些功能部分手机不能使用,网站,通讯录,wifi基本上每个手机都可以使用. 在看之前你可以扫一扫下面几个二维码先看看效果: 1.二维码生成 网址 (URL) 包含网址的 二维码生成 是大家平时最常接触 ...
- 在线浏览PDF之PDF.JS (附demo)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#skill 下载地址:http://mozilla.gith ...
- 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo
Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...
- vue双向数据绑定原理探究(附demo)
昨天被导师叫去研究了一下vue的双向数据绑定原理...本来以为原理的东西都非常高深,没想到vue的双向绑定真的很好理解啊...自己动手写了一个. 传送门 双向绑定的思想 双向数据绑定的思想就是数据层与 ...
- Android Studio-—使用OpenCV的配置方法和demo以及开发过程中遇到的问题解决
前提: 1.安装Android Studio(过程略) 2.官网下载OpenCV for Android 网址:http:opencv.org/downloads.html 我下载的是下图的版本 3. ...
- iOS之ProtocolBuffer搭建和示例demo
这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...
- 钉钉开放平台demo调试异常问题解决:hostname in certificate didn't match
今天研究钉钉的开放平台,结果一个demo整了半天,这帮助系统写的也很难懂.遇到两个问题: 1.首先是执行demo时报unable to find valid certification path to ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
随机推荐
- .NET-架构优化实战-梳理篇
原文:.NET-架构优化实战-梳理篇 前言 程序员输出是他敲写的代码,那么输入就是他思考好的设计.因此不做设计是不存在,设计只分优秀的设计和糟糕的设计.为了避免过度设计浪费成本,需要针对现有业务与问题 ...
- cors-synchronous-requests-not-working-in-firefox
http://stackoverflow.com/questions/16668386/cors-synchronous-requests-not-working-in-firefox
- Hdu4771(杭州赛区)
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- 1069. The Black Hole of Numbers (20)【模拟】——PAT (Advanced Level) Practise
题目信息 1069. The Black Hole of Numbers (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B For any 4-digit inte ...
- active set method(激活集方法)
在优化问题的求解中,如果待优化(最大最小)的目标函数,其解集受限于一组约束条件, g1(x)≥0,-,gk(x)≥0 约束条件定义着可行域(feasible region),对于可行域中的任一点 x ...
- NOIP模拟 - 树
题目描述 给出一张n个点,m条边的无向图,摧毁每条边都需要一定的体力,并且花费的体力值各不相同,给定图中两个点x,y(x≠y),每当(x,y)之间存在路径,就需要不断摧毁当前图中花费体力最少的一条边, ...
- JQuery 多个checkbox 只选中一个
<form id="common-form"> <input name="check1" type="checkbox"/ ...
- padding百分百值是相对谁的百分比
先看代码 <head> <meta charset="UTF-8"> <title>Document</title> <sty ...
- js判断两个时间段是否有交集
//判断两个时间是否有交集 function isDateIntersection(start1, end1, start2, end2) { var startdate1 = new Date(st ...
- Method and system for providing security policy for linux-based security operating system
A system for providing security policy for a Linux-based security operating system, which includes a ...