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:

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的更多相关文章

  1. TMsgThread, TCommThread -- 在delphi线程中实现消息循环

    http://delphi.cjcsoft.net//viewthread.php?tid=635 在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使 ...

  2. TMsgThread, TCommThread -- 在delphi线程中实现消息循环(105篇博客,好多研究消息的文章)

    在delphi线程中实现消息循环 在delphi线程中实现消息循环 Delphi的TThread类使用很方便,但是有时候我们需要在线程类中使用消息循环,delphi没有提供.   花了两天的事件研究了 ...

  3. delphi 线程教学第六节:TList与泛型

    第六节: TList 与泛型   TList 是一个重要的容器,用途广泛,配合泛型,更是如虎添翼. 我们先来改进一下带泛型的 TList 基类,以便以后使用. 本例源码下载(delphi XE8版本) ...

  4. Delphi线程定时器TThreadedTimer及用法--还有TThreadList用法可以locklist

    Delphi线程定时器 - -人生如歌- - 博客园http://www.cnblogs.com/zhengwei0113/p/4192010.html (* 自己编写的线程计时器,没有采用消息机制, ...

  5. delphi 线程教学第二节:在线程时空中操作界面(UI)

    第二节:在线程时空中操作界面(UI)   1.为什么要用 TThread ?   TThread 基于操作系统的线程函数封装,隐藏了诸多繁琐的细节. 适合于大部分情况多线程任务的实现.这个理由足够了吧 ...

  6. delphi 线程教学第一节:初识多线程

    第一节:初识多线程   1.为什么要学习多线程编程?   多线程(多个线程同时运行)编程,亦可称之为异步编程. 有了多线程,主界面才不会因为耗时代码而造成“假死“状态. 有了多线程,才能使多个任务同时 ...

  7. 多线程的基本概念和Delphi线程对象Tthread介绍

    多线程的基本概念和Delphi线程对象Tthread介绍 作者:xiaoru    WIN 98/NT/2000/XP是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用CPU运行 ...

  8. Delphi线程的终止

    当线程对象的Execute()执行完毕,我们就认为此线程终止了.这时候,它会调用Delphi的一个标准例程EndThread(),这个例程再调用API函数ExitThread().由ExitThrea ...

  9. Delphi线程基础知识

    参考http://blog.chinaunix.net/uid-10535208-id-2949323.html 一.概述 Delphi提供了好几种对象以方便进行多线程编程.多线程应用程序有以下几方面 ...

随机推荐

  1. Why I am not afraid of AI (TBC)

    Freud! Yes, according to Freud's theory, most human activities are driven by libido (or aim-inhibite ...

  2. Java 多线程 2015/9/21

    http://lavasoft.blog.51cto.com/62575/27069   http://blog.csdn.net/aboy123/article/details/38307539   ...

  3. 在mac os x 下升级emacs

    大概是09年的时候接触到emacs这个编辑器,当时我们c语言老师用的,他自信满满,而那时我是个vimer,所以每次看他按那么多组合键我就替他感觉手指头累啊. 再后来我用了几年vim写代码,再后来用了许 ...

  4. ajax完成团队信息异步添加【实际项目】

    第一:ajax往后台传参如何串(目前理解是json数组直接传给对象) 第二:ajax返回的数值通过PrintWriter.print方法返回 [参考前台页面关于团队信息是如何实现的] 参考页面user ...

  5. BZOJ2820 YY的GCD 【莫比乌斯反演】

    BZOJ2820 YY的GCD Description 神犇YY虐完数论后给傻×kAc出了一题给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, ...

  6. selenium 上传文件方法补充——SendKeys、win32gui

    之前和大家说了input标签的上传文件的方式: <selenium2 python 自动化测试实战>(13)——上传文件 现在好多网站上传的标签并不是input,而是div之类的比如: 全 ...

  7. [LOJ6145][2017 山东三轮集训 Day7]Easy

    loj description 一棵树,每次给出\(l,r,x\),求从点\(x\)出发到达\([l,r]\)中任意一点的最短距离. sol 动态点分治. 建出点分树后,在每个节点上用以点编号为下标的 ...

  8. ALTERA的FPGA命名规则

    DIP中文解释:双列直插式封装.插装型封装之一,引脚从封装两侧引出,封装材料有塑料和陶瓷两种.DIP是最普及的插装型封装,应用范围包括标准逻辑IC,存贮器LSI,微机电路等.        PLCC中 ...

  9. ORM 查询

    ORM版学员管理系统 班级表 表结构 class Class(models.Model): id = models.AutoField(primary_key=True) # 主键 cname = m ...

  10. oracle之 oracle database vault(数据库保险库)

    在12c建库中 Database  Vault 与 Label Security 选项,之前没有留意过,特意记录一下 12.1 中: 12.2 中: 转载:http://www.linuxidc.co ...