delphi 线程 TTask
2016-01-25 14:34
447 查看
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:
C++:
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:
C++:
TTaskStatus = (Created, WaitingToRun, Running, Completed, WaitingForChildren, Canceled, Exception);
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 - 1 downto 0 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 - 1].Start;
for i := listTask.Count - 1 downto 0 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(500); 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 funtion
- delphi 年月日自动加一减一函数,月加一,月减一
- delphi 如何判断编码格式,解决乱码问题
- Delphi2010Ping某IP是否连通的代码实现
- Delphi 调用Codesoft打印条码
- Delphi 调用BarTender打印条码
- 使用delphi+intraweb进行微信开发3—微信消息处理
- Delphi2010 RTTI + Attribute 简单实现ORM实例
- delphi IOS 简单类型转换
- DelphiXE Android自适应屏幕办法
- Delphi使用JSON
- delphi文件操作
- Delphi-Edit中只能输入数字且只能输入一个小数点
- Delphi XE8 程序瘦身.编译后EXE最低90KB.
- 【Delphi】GIF 动画建立
- FireDAC简易教程
- Delphi XE7的安卓程序如何调用JAVA的JAR,使用JAVA的类?
- delphi7 编译程序时报win32.indcu.a病毒的解决方法
- Delphi PChar与String互转
- Delphi+MySQL:TADOQuery使用插入中文乱码解决方法