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. 兼容iOs7的自定义alertView

    转载请注明出处. 升级到ios7后,旧项目中使用的继承UIAlertView的自定义alertview无法正常显示了,无奈只好换思路去实现,改成从当前keywindow下创建要显示的alertview ...

  2. Jmeter-JDBC Connection Configuration和JDBC Request注释

    1.JDBC Connection Configuration Variable Name--变量名,唯一值不可重复,给JDBC Request确定使用哪个配置 Max Number of Conne ...

  3. 将 WPF、UWP 以及其他各种类型的旧样式的 csproj 文件迁移成新样式的 csproj 文件

    写过 .NET Standard 类库或者 .NET Core 程序的你一定非常喜欢微软为他们新开发的项目文件(对于 C#,则是 csproj 文件).这种文件非常简洁,组织一个庞大的项目也只需要聊聊 ...

  4. 从 “x is null 和 x == null” 的区别看 C# 7 模式匹配中常量和 null 的匹配

    尝试过写 if (x is null)?它与 if (x == null) 相比,孰优孰劣呢? x is null 还有 x is constant 是 C# 7.0 中引入的模式匹配(Pattern ...

  5. static类和no static类的区别

    1.static类,只能有静态成员,不能被实例.静态的东西在内存中只有一份,调用速度会快,但是相对费内存. 2.在另外一个类内部定义的类,此类的实例化不希望依赖外部类的实例化,此时可以定义为静态类(即 ...

  6. CentOS 7.0 yum安装Apache、PHP和MySQL

    centos7默认将mariadb视作mysql. p.s.因为mysql被oracle收购后,原作者担心mysql闭源,所以又写了一个mariadb,这个数据库可以理解为mysql的分支. 卸载ma ...

  7. tomcat  nginx  证书切换

    1. 导出公钥 keytool -export -alias tomcat -keystore <you jks>wsriakey.keystore -file <outputfil ...

  8. iOS7 自己定义动画跳转

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/Liar0606/article/details/26399125 简单介绍 在iOS7系统中,假设你 ...

  9. 系列文章--ASP.NET之AJAX入门教程

    ASP.NET AJAX入门系列将会写关于ASP.NET AJAX一些控件的使用方法以及基础知识,其中部分文章为原创,也有一些文章是直接翻译自官方文档,本部分内容会不断更新. 目录 ASP.NET A ...

  10. pthread中读写锁

    读写锁很像一个互斥量,他阻止多个线程同时修改共享数据的另一种方法,区分不同互斥量的是他是分读数据和写数据,一个读写锁允许同时多个线程读数据,只要他们不修改数据. 只要没有写模式下的加锁,任意线程都可以 ...