Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Catalog · How the catalog works
Anything · Lazarus or Delphi
Random projectRandom project

AnonymousThreads

View on GitHub

Member rating

No ratings yet.

Sign in to rate this project.

README

TThreadCustom for Free Pascal / Lazarus

An elegant and robust anonymous thread implementation for Free Pascal/Lazarus, inspired by Delphi's TThread.CreateAnonymousThread. Make multi-threaded development easier and safer!


🚀 Features

  • Compatible: Free Pascal 3.2.3+
  • Delphi-like interface: Easy code migration
  • Structured callbacks: OnShow, OnProcess, OnComplete, OnFinally, OnError
  • Automatic Synchronize: UI callbacks run in the main thread
  • Cancellation control: Supports Terminate and CheckTerminated
  • Robust exception handling: OnError receives exception message
  • Auto-destruction: FreeOnTerminate := True automatically
  • No external dependencies: Only Classes and SysUtils

📦 Installation

  1. Copy the uThread.pas file to your project
  2. Add uThread to your uses clause
  3. Ready to go!
pascal
uses
  uThread;

💡 Basic Usage Example

pascal
procedure TForm1.Button1Click(Sender: TObject);
begin
  TThreadCustom.Start(
    @ShowProgress,    // OnShow (UI thread)
    @DoHeavyWork,     // OnProcess (background)
    @WorkComplete,    // OnComplete (UI thread)
    @WorkFinally,     // OnFinally (UI thread)
    @HandleError,     // OnError (UI thread)
    True              // Execute OnComplete even on error
  );
end;

🔄 Cancellation Control

pascal
private
  FCurrentThread: TThreadCustom;

procedure TForm1.StartLongTask;
begin
  FCurrentThread := TThreadCustom.Start(
    @ShowTaskStart,
    @DoLongTask,
    @TaskComplete,
    @TaskFinally,
    @TaskError,
    True
  );
end;

procedure TForm1.CancelTask;
begin
  if Assigned(FCurrentThread) and not FCurrentThread.IsTerminated then
  begin
    FCurrentThread.Terminate;
    ShowMessage('Cancelling...');
  end;
end;

🔧 API Reference

pascal
class function Start(
  AOnShow, AOnProcess, AOnComplete: TProc;
  AOnFinally: TProcBoolean; 
  AOnError: TProcString = nil;
  const ADoCompleteWithError: Boolean = True
): TThreadCustom;

Callback Types:

  • TProc = procedure of object;
  • TProcBoolean = procedure(AIsTerminated: Boolean) of object;
  • TProcString = procedure(const AExceptionMessage: string) of object;

Properties:

  • IsTerminated: Boolean

Methods:

  • Terminate

🎯 Usage Examples

  • File processing
  • Data download
  • Database operations

⚠️ Important Notes

✅ Do's

  • Declare callbacks as class methods (procedure of object)
  • Use @MethodName to reference callbacks
  • Access UI only in OnShow, OnComplete, OnFinally, and OnError
  • Use TThread.CheckTerminated in long loops
  • Handle exceptions properly

❌ Don'ts

  • Do not access visual components in OnProcess
  • Do not use inline anonymous procedures (not supported in FPC 3.2.3)
  • Do not use WaitFor or FreeAndNil (destruction is automatic)

🔄 Delphi → FPC/Lazarus Migration

Delphi:

pascal
TThread.CreateAnonymousThread(
  procedure
  begin
    // work here
    TThread.Synchronize(nil, 
      procedure
      begin
        // update UI
      end);
  end
).Start;

FPC/Lazarus:

pascal
TThreadCustom.Start(
  nil,           // OnShow
  @DoWork,       // OnProcess
  @UpdateUI,     // OnComplete  
  nil,           // OnFinally
  @HandleError,  // OnError
  True
);

📋 Requirements

  • Free Pascal 3.2.0 or higher
  • Lazarus (any recent version)
  • Platforms: Windows, Linux, macOS

🤝 Contributing

Contributions are welcome!

  1. Fork the project
  2. Create a feature branch: git checkout -b feature/YourFeature
  3. Commit your changes: git commit -m 'Add YourFeature'
  4. Push to the branch: git push origin feature/YourFeature
  5. Open a Pull Request

📄 License

This project is licensed under the MIT License. See the LICENSE file for details.