Extended Delphi threads to transfer parameters.

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The new Delphi thread TThread with CreateAnonymousThread method, if you add to it a transitive parameter is not better? The code as follows:

  TAnonymousThreadX<T> = class(TThread)
  private
    FProc: TProc<T>;
    AValue:T;
  protected
    procedure Execute; override;
  public
    constructor Create(const AProc: TProc<T>;ProcPar:T);
  end;

TThreadHelper= class helper for TThread public class function CreateAnonymousThreadX<T>(const ThreadProc: TProc<T>;proPar:T): TThread; static; end; implementation { TAnonymousThreadX } constructor TAnonymousThreadX<T>.Create(const AProc: TProc<T>;ProcPar:T); begin inherited Create(True); FreeOnTerminate := True; FProc := AProc; Avalue:=ProcPar; end; procedure TAnonymousThreadX<T>.Execute; begin inherited; FProc(Avalue); end; { TThreadHelper } class function TThreadHelper.CreateAnonymousThreadX<T>(const ThreadProc: TProc<T>; proPar: T): TThread; begin Result := TAnonymousThreadX<T>.Create(ThreadProc,proPar); end;

Code is very simple, is the transfer of a method and process parameters. The online call it.

The above code is saved in a unit, references, can be used this way:

//Such use
TThread.CreateAnonymousThreadX<Integer>(TestX,1234).Start;

//This is the body of the TestX method
procedure T***.TestX(Avalue: Integer);
begin
  btnContinue.Caption:=IntToStr(Avalue);
end;


//If you do not want to define the TestX method, also can directly call the following methods
  TThread.CreateAnonymousThreadX<Integer>(
  procedure(Avalue:Integer)
  begin
    btnContinue.Caption:=IntToStr(Avalue);
  end
  ,12345).Start;

If the method is executing thread has two, three parameters, the control to change it.


This thread is it right? More easier.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Harvey at November 16, 2013 - 8:54 PM