An elegant and robust anonymous thread implementation for Free Pascal/Lazarus, inspired by Delphi's TThread.CreateAnonymousThread. Make multi-threaded development easier and safer!
- 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
- Copy the
uThread.pasfile to your project - Add
uThreadto your uses clause - Ready to go!
uses
uThread;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;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;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
- File processing
- Data download
- Database operations
- Declare callbacks as class methods (
procedure of object) - Use
@MethodNameto reference callbacks - Access UI only in OnShow, OnComplete, OnFinally, and OnError
- Use
TThread.CheckTerminatedin long loops - Handle exceptions properly
- 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:
TThread.CreateAnonymousThread(
procedure
begin
// work here
TThread.Synchronize(nil,
procedure
begin
// update UI
end);
end
).Start;FPC/Lazarus:
TThreadCustom.Start(
nil, // OnShow
@DoWork, // OnProcess
@UpdateUI, // OnComplete
nil, // OnFinally
@HandleError, // OnError
True
);- Free Pascal 3.2.0 or higher
- Lazarus (any recent version)
- Platforms: Windows, Linux, macOS
Contributions are welcome!
- Fork the project
- Create a feature branch:
git checkout -b feature/YourFeature - Commit your changes:
git commit -m 'Add YourFeature' - Push to the branch:
git push origin feature/YourFeature - Open a Pull Request
This project is licensed under the MIT License. See the LICENSE file for details.