delphi 线程 TTask
http://docwiki.embarcadero.com/Libraries/Seattle/en/System.Threading.TTask
http://docwiki.embarcadero.com/Libraries/Seattle/en/System.Threading.TTaskStatus
The Parallel Programming Library (PPL) provides a TTask class to run one task or multiple tasks in parallel. A Task is a unit of work you need to get done. The PPL does the association between the task and the thread that performs the task so you can run several tasks in parallel without having to create your own custom threads and managing them.
TTask creates and manages interaction with instances of ITask. ITask is an interface that provides a range of methods and properties to Start, Wait, Cancel and also a property for Status (Created, WaitingToRun, Running, Completed, WaitingForChildren, Canceled, Exception).
TTask provides WaitForAll and WaitForAny to wait for the completion of all or any tasks. WaitForAll returns when all of the tasks are completed, whereas WaitForAny tells you the first one that is completed. For example, if you have two tasks A and B which take 3 and 5 seconds respectively, the time for you to get a result is:
- Sequentially (not using TTask/ITask) = 8 seconds
- TTask.WaitForAny = 3 seconds
- TTask.WaitForAll = 5 seconds
The following example uses the WaitForAll method:
Delphi:
procedure TFormThreading.MyButtonClick(Sender: TObject);
var
tasks: array of ITask;
value: Integer;
begin
Setlength (tasks ,2);
value := 0;
tasks[0] := TTask.Create (procedure ()
begin
sleep (3000); // 3 seconds
TInterlocked.Add (value, 3000);
end);
tasks[0].Start;
tasks[1] := TTask.Create (procedure ()
begin
sleep (5000); // 5 seconds
TInterlocked.Add (value, 5000);
end);
tasks[1].Start;
TTask.WaitForAll(tasks);
ShowMessage ('All done: ' + value.ToString);
end;
C++:
void __fastcall TFormThreading::MyButtonClick(TObject *Sender)
{
_di_ITask tasks[2];
tasks[0] = TTask::Create(_di_TProc(new TCppTask(lvalue, 3000, Label1)));
tasks[0]->Start());
tasks[1] = TTask::Create(_di_TProc(new TCppTask(lvalue, 5000, Label1)));
tasks[1]->Start());
TTask::WaitForAll(tasks,(sizeof(tasks)/sizeof(tasks[0])-1));
ShowMessage("All done! "+IntToStr(lvalue));
}
Another functionality of TTask is to prevent the user interface locking up if you want to start something in the background. The following code example shows how to run a single task and start it:
Delphi:
procedure TFormThreading.Button1Click(Sender: TObject);
var
aTask: ITask;
begin
aTask := TTask.Create (procedure ()
begin
sleep (3000); // 3 seconds
ShowMessage ('Hello');
end);
aTask.Start;
end;
C++:
void __fastcall TFormThreading::Button1Click(TObject *Sender)
{
Label1->Caption = "--";
lvalue = 0;
_di_ITask aTask = TTask::Create(_di_TProc(new TCppTask(lvalue,3000,Label1)));
aTask-> Start();
Label1->Caption =InToStr(lvalue);
} aITask.Status
GetStatus
TTaskStatus = (Created, WaitingToRun, Running, Completed, WaitingForChildren, Canceled, Exception);
listTask: TList<ITask>; for i := listTask.Count - downto do
begin
if (listTask.Items[i].Status = TTaskStatus.Completed) then
begin
listTask.Delete(i);
end;
end; listTask.Add(TTask.Create(self.mythread));
listTask.Items[listTask.Count - ].Start;
for i := listTask.Count - downto do
begin
if listTask.Items[i].Status in [TTaskStatus.Running, TTaskStatus.Created, TTaskStatus.WaitingToRun] then
begin
while not(listTask.Items[i].Status in [TTaskStatus.Completed, TTaskStatus.Canceled, TTaskStatus.Exception]) do
begin
Sleep();
Application.ProcessMessages;
end;
end;
end;
Label1.Text:='disConnect OK!!'
for i := listTask.Count - 1 downto 0 do
begin
listTask.Items[i].wait(5000); end;
delphi 线程 TTask的更多相关文章
- TMsgThread, TCommThread -- 在delphi线程中实现消息循环
http://delphi.cjcsoft.net//viewthread.php?tid=635 在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使 ...
- TMsgThread, TCommThread -- 在delphi线程中实现消息循环(105篇博客,好多研究消息的文章)
在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使用很方便,但是有时候我们需要在线程类中使用消息循环,delphi没有提供. 花了两天的事件研究了 ...
- delphi 线程教学第六节:TList与泛型
第六节: TList 与泛型 TList 是一个重要的容器,用途广泛,配合泛型,更是如虎添翼. 我们先来改进一下带泛型的 TList 基类,以便以后使用. 本例源码下载(delphi XE8版本) ...
- Delphi线程定时器TThreadedTimer及用法--还有TThreadList用法可以locklist
Delphi线程定时器 - -人生如歌- - 博客园http://www.cnblogs.com/zhengwei0113/p/4192010.html (* 自己编写的线程计时器,没有采用消息机制, ...
- delphi 线程教学第二节:在线程时空中操作界面(UI)
第二节:在线程时空中操作界面(UI) 1.为什么要用 TThread ? TThread 基于操作系统的线程函数封装,隐藏了诸多繁琐的细节. 适合于大部分情况多线程任务的实现.这个理由足够了吧 ...
- delphi 线程教学第一节:初识多线程
第一节:初识多线程 1.为什么要学习多线程编程? 多线程(多个线程同时运行)编程,亦可称之为异步编程. 有了多线程,主界面才不会因为耗时代码而造成“假死“状态. 有了多线程,才能使多个任务同时 ...
- 多线程的基本概念和Delphi线程对象Tthread介绍
多线程的基本概念和Delphi线程对象Tthread介绍 作者:xiaoru WIN 98/NT/2000/XP是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用CPU运行 ...
- Delphi线程的终止
当线程对象的Execute()执行完毕,我们就认为此线程终止了.这时候,它会调用Delphi的一个标准例程EndThread(),这个例程再调用API函数ExitThread().由ExitThrea ...
- Delphi线程基础知识
参考http://blog.chinaunix.net/uid-10535208-id-2949323.html 一.概述 Delphi提供了好几种对象以方便进行多线程编程.多线程应用程序有以下几方面 ...
随机推荐
- 前端ajax异步传值以及后端接收参数的几种方式
原文参考 异步传值 前台往后台传值呢,有很多种方式,大家听我细细道来. 第一种呢,也是最简单的一种,通过get提交方式,将参数在链接中以问号的形式进行传递. // 前台传值方法 // 触发该方法调用a ...
- 每天一个linux命令:【转载】ls命令
ls命令是linux下最常用的命令.ls命令就是list的缩写,缺省下ls用来打印出当前目录的清单,如果ls指定其他目录,那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...
- 使用python处理selenium中的frame切换问题
# iframe有name或id值 self.driver.switch_to.frame('iframe-name-id') # iframe没有name或id值 xf = self.driver. ...
- 重温CLR(十) 字符、字符串和文本处理
本章将介绍.net中处理字符和字符串的机制 字符 在.NET Framewole中,字符总是表示成16位Unicode代码值,这简化了国际化应用程序的开发. 每个字符都表示成System.Char结构 ...
- 《selenium2 python 自动化测试实战》(3)——操作测试对象
上一节我们说了如何定位元素,定位到元素以后就涉及到对元素的操作了,webdriver中常用的操作元素的方法有: clear ——用于清除输入框的默认内容 send_keys ——用于在一个输入框里 ...
- matplotlib ----- 初步
直接看几段代码即可: # 加载模块的方式 import matplotlib.pyplot as plt import numpy as np # 最简单的单线图 x = np.linspace(0, ...
- phoenix elixir 框架简单试用
备注: 官方提供的脚手架工具,我们可以直接使用,生成代码,同时需要nodejs 环境配置(比较简单,参考 相关资料即可) 1. 安装脚手架 mix archive.install https:/ ...
- 解决asp.net上传文件时文件太大导致的错误
即使在web.config中添加了节点和设置依然是不行的,还是报文件太大的错误, <httpModules> <add name="UploadHttpModu ...
- 编译openvpn在链接时报tuncfg错误
1. 链接时产生tuncfg错误 init.o: In function `do_persist_tuntap': init.c:(.text+0x24d8): undefined reference ...
- Mac环境下终端(Terminal)用ssh 连接服务器问题 Received disconnect from 120.55.x.x: 2: Too many authentication failures for root
由于这台Mac配置git生成公钥后,ssh连接就出现来这个问题 Received disconnect from 120.55.x.x: 2: Too many authentication fail ...