Root > Advanced topics > Multi-threaded applications > Creating threads > Anonymous threads

Anonymous threads

Previous pageReturn to chapter overviewNext page   

Anonymous thread is a wrapper for TThread class which simplifies "fire-and-forget" approach. Creating new threads with bare TThread class is complex: you have to create your own thread class. And TThread is not capable of running arbitrary code, so you can't run just functions in threads. Another advantage of anonymous thread is capturing local variables.

 

So, for example, such code with TThread class:

 

type
  TMyThread = class(TThread)
  private
    FLocalVar: TSomeType;
  protected
    procedure Execute; override;
  public
    property LocalVar: TSomeType read FVar write FVar;
  end;
 
procedure TMyMotile.Execute;
begin
  // ... some code which uses LocalVar
end;
 
...
 
var
  Thread: TMyThread;
  LocalVar: TSomeType;
begin
  Thread := TMyMotile.Create(True);
  Thread.LocalVar := LocalVar;
  Thread.FreeOnTerminate := True;
  Thread.Start;
end;

 

can be replaced with shorter version of the code which uses anonymous thread:

 

var
  LocalVar: TSomeType;
begin
  TThread.CreateAnonymousThread(
    procedure;
    begin
      // ... some code which uses LocalVar
    end).Start;
end;

 

Internally, anonymous thread is implemented as TAnonymousThread class which is descendant from TThread class. Instances of TAnonymousThread are created via CreateAnonymousThread class method. The whole code for anonymous threads is very simple:

 

type
  TAnonymousThread = class(TThread)
  private
    FProc: TProc;
  protected
    procedure Execute; override;
  public
    constructor Create(const AProc: TProc);
  end;
 
{ TAnonymousThread }
 
constructor TAnonymousThread.Create(const AProc: TProc);
begin
  inherited Create(True);
  FreeOnTerminate := True;
  FProc := AProc;
end;
 
procedure TAnonymousThread.Execute;
begin
  FProc();
end;
 
...
 
class function TThread.CreateAnonymousThread(const ThreadProc: TProc): TThread;
begin
  Result := TAnonymousThread.Create(ThreadProc);
end;

 

Note: anonymous threads are created in suspended state, so you must run them manually with Start method. Suspended state allows you to alter thread's properties or pass additional parameters before running the thread.

 

Important notes:

anonymous threads are created with FreeOnTerminate property set to True. This means that you should never access thread variable after you have called Start method.

 

TAnonymousThread class have no additional features except being able to accept arbitrary anonymous function to be executed in its Execute method. Since anonymous threads are based on TThread class - the error handling for anonymous thread is the same as for the TThread class - that is, five different methods. Obviously, you can not override DoTerminate method (since you do not declare a class), but all other exception handling methods are fully applicable for anonymous threads.

 

Important notes:

EurekaLog has to be enabled for background threads.
it is recommended to use TThreadEx class instead of TThread class to create anonymous threads. Anonymous threads created with TThreadEx class supports AutoHandleException property - which provides one more method to handle exceptions for anonymous threads.

 

 

See also:




Send feedback... Build date: 2018-11-26
Last edited: 2018-11-08
PRIVACY STATEMENT
The documentation team uses the feedback submitted to improve the EurekaLog documentation. We do not use your e-mail address for any other purpose. We will remove your e-mail address from our system after the issue you are reporting has been resolved. While we are working to resolve this issue, we may send you an e-mail message to request more information about your feedback. After the issues have been addressed, we may send you an email message to let you know that your feedback has been addressed.


Permanent link to this article: https://www.eurekalog.com/help/eurekalog/multithreading_anonymous.php