Execute a method or static procedure in a thread.
Source position: classesh.inc line 2380
public class function TThread.ExecuteInThread( |
AMethod: TThreadExecuteHandler; |
AOnTerminate: TNotifyEvent = Nil |
AMethod: TThreadExecuteStatusHandler; |
AOnStatus: TThreadStatusNotifyEvent; |
AOnTerminate: TNotifyEvent = Nil |
AMethod: TThreadExecuteCallBack; |
AData: Pointer = Nil; |
AOnTerminate: TNotifyCallBack = Nil |
AMethod: TThreadExecuteStatusCallBack; |
AOnStatus: TThreadStatusNotifyCallBack; |
AData: Pointer = Nil; |
AOnTerminate: TNotifyCallBack = Nil |
):TThread; overload; |
TThread.ExecuteInThread is a class method which allows to quickly execute a method or procedure in a thread. The method or procedure to be executed is passed in Method, this can be a method or a plain (static) procedure.
The caller can be notified of thread termination: In the optional argument AOnTerminate a callback (procedure or method, depending on the signature) can be specified that will be called when the thread terminated. This callback is executed in the main thread.
The signature of AMethod determines whether status reporting is enabled or not. If the method of type TThreadExecuteStatusHandler or TThreadExecuteStatusCallback, then an extra AOnStatus callback must be specified. This callback will be called in the main thread whenever the thread wishes to be report its status. The status callback should not do extensive work, because while the status callback is called, thread execution is suspended.
When using a plain procedure, extra data can be passed on to the procedure in AData. The AData pointer will be passed to the thread method, and also to the thread status callback and thread termination callback.
|
Signature of method to execute in a thread, without status reporting. |
|
|
Signature of method to execute in a thread, with status reporting. |
|
|
Signature of procedure to execute in a thread, with status reporting. |
|
|
Signature of procedure to execute in a thread, without status reporting. |
program tthrc; uses cthreads, sysutils, classes; Var D : Integer; Procedure DoneThread(Sender : TObject; AData : Pointer); begin Writeln('Thread ',TThread(Sender).ThreadID,' done. D is currently: ', PInteger(AData)^); end; Procedure DoThread(AData : Pointer); Var I : integer; begin for I:=1 to 10 do begin Sleep(10*Random(30)); Writeln('Thread ',TThread.CurrentThread.ThreadID,' ping ',I); Inc(PInteger(AData)^,i); end; end; Var T1,T2 : TThread; begin T1:=TThread.ExecuteInThread(@DoThread,@D,@DoneThread); T2:=TThread.ExecuteInThread(@DoThread,@D,@DoneThread); Writeln('Main thread done'); T1.WaitFor; T2.WaitFor; end.
program tthre; {$mode objfpc} {$H+} uses cthreads, sysutils, classes; Type TTestThread = Class(TObject) D : Integer; Procedure DoneThread(Sender : TObject); Procedure DoThread; Procedure Run; end; Procedure TTestThread.DoneThread(Sender : TObject); begin Writeln('Thread ',TThread(Sender).ThreadID,' done. D is currently: ', D); end; Procedure TTestThread.DoThread; Var I : integer; begin for I:=1 to 10 do begin Sleep(10*Random(30)); Writeln('Thread ',TThread.CurrentThread.ThreadID,' ping ',I); Inc(D,i); end; end; Procedure TTestThread.Run; Var T1,T2 : TThread; begin T1:=TThread.ExecuteInThread(@DoThread,@DoneThread); T2:=TThread.ExecuteInThread(@DoThread,@DoneThread); Writeln('Main thread done'); T1.WaitFor; T2.WaitFor; end; begin With TTestThread.Create do try Run; finally Free; end; end.
program tthrcs; {$h+} uses cthreads, sysutils, classes; Var D : Integer; DoneThreads : INteger; Procedure DoneThread(Sender : TObject; Data : Pointer); begin Inc(DoneThreads); Writeln('Thread ',TThread(Sender).ThreadID,' done. D is currently: ', PInteger(Data)^); end; Procedure ReportThreadStatus(Sender : TThread; AData : Pointer;Const status : String); begin Writeln('Thread ',Sender.ThreadID,' Status report : ',Status); end; Procedure DoThread(AData : Pointer; Report : TThreadReportStatus); Var I : integer; begin for I:=1 to 10 do begin Sleep(10*Random(30)); Report('Ping '+IntToStr(i)); Inc(PInteger(AData)^,i); end; end; Var T1,T2 : TThread; begin DoneThreads:=0; T1:=TThread.ExecuteInThread(@DoThread,@ReportThreadStatus,@D,@DoneThread); T2:=TThread.ExecuteInThread(@DoThread,@ReportThreadStatus,@D,@DoneThread); Writeln('Main thread loop'); While DoneThreads<2 do begin Sleep(10); CheckSynchronize; end; T1.WaitFor; T2.WaitFor; end.
program tthrc; {$mode objfpc} {$H+} uses cthreads, sysutils, classes; Type TTestThread = Class(TObject) D : Integer; DoneThreads : integer; Procedure DoneThread(Sender : TObject); Procedure ReportThreadStatus(Sender : TThread; Const status : String); Procedure DoThread(Report: TThreadReportStatus); Procedure Run; end; Procedure TTestThread.DoneThread(Sender : TObject); begin Inc(DoneThreads); Writeln('Thread ',TThread(Sender).ThreadID,' done. D is currently: ', D); end; Procedure TTestThread.ReportThreadStatus(Sender : TThread; Const status : String); begin Writeln('Thread ',Sender.ThreadID,' Status report : ',Status); end; Procedure TTestThread.DoThread(Report : TThreadReportStatus); Var I : integer; begin for I:=1 to 10 do begin Sleep(10*Random(30)); Report('Ping '+IntToStr(i)); Inc(D,i); end; end; Procedure TTestThread.Run; Var T1,T2 : TThread; begin DoneThreads:=0; T1:=TThread.ExecuteInThread(@DoThread,@ReportThreadStatus,@DoneThread); T2:=TThread.ExecuteInThread(@DoThread,@ReportThreadStatus,@DoneThread); Writeln('Main thread loop'); While DoneThreads<2 do begin Sleep(10); CheckSynchronize; end; T1.WaitFor; T2.WaitFor; end; begin With TTestThread.Create do try Run; finally Free; end; end.