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提供了好几种对象以方便进行多线程编程.多线程应用程序有以下几方面 ...
随机推荐
- >=< 在set和dict中的不同
两个dict比较的算法是,长度>键>值,由于dict无序,所以比较的时候会自动将键对齐比较,我们不用担心这个. >>> d1 = dict(x=1, y=2) >& ...
- 从BZOJ2242看数论基础算法:快速幂,gcd,exgcd,BSGS
LINK 其实就是三个板子 1.快速幂 快速幂,通过把指数转化成二进制位来优化幂运算,基础知识 2.gcd和exgcd gcd就是所谓的辗转相除法,在这里用取模的形式体现出来 \(gcd(a,b)\) ...
- sublime自动格式化代码插件HTML-CSS-JS Prettify安装
sublime自动格式化代码插件HTML-CSS-JS Prettify安装 问题: 用 Sublime Text 格式化代码(安装 HTML-CSS-JS Prettify 插件)时,格式化时却会提 ...
- java 线程池--ExecutorService
一 Java通过Executors提供四种线程池,分别为: newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程. new ...
- python调用rpc实现分布式系统
rpc 一般俗称,远程过程调用,把本地的函数,放到远端去调用. 通常我们调用一个方法,譬如: sumadd(10, 20),sumadd方法的具体实现要么是用户自己定义,要么存在于该语言的库函数中,也 ...
- centos 限制只能访问某个目录的php文件
vi /etc/php.ini #编辑 open_basedir = .:/tmp/ #在380行 设置表示允许访问当前目录(即PHP脚本文件所在之目录)和/tmp/目录,可以防止php木马跨站,如果 ...
- Java NIO简单介绍(二)
上一篇<NIO简单介绍(一)>中讲解了NIO中本地IO相关的内容,这篇重点介绍的NIO的非阻塞式网络通信 一.阻塞与非阻塞 传统的 IO 流都是阻塞式的.也就是说,当一个线程调用 read ...
- Nexus搭建私服
什么是Nexus Nexus是一个强大的Maven仓库管理器,它极大地简化了本地内部仓库的维护和外部仓库的访问. 运行原理 本地仓库与私服处在同一个局域网中,当本地仓库没有资源时,会向私服发起请求获取 ...
- requirejs 到底有什么好处?
无论是在backbone时代,还是angularjs 时代 我都用过requirejs, 后来慢慢全都去掉了, 因为在前端开发requirejs 感觉没有带来任何实质性的好处. 从几个方面说说我的感受 ...
- stl map一对多用法
// stlMap.cpp : Defines the entry point for the console application.//#pragma warning (disable : 478 ...