TFuture_Async_Delphi_Example
TFuture_Async_Delphi_Example
1. What is TFuture<T>?
TFuture<T> is part of the System.Threading unit and implements the IFuture<T> interface.
Basic pattern:
- Use TTask.Future<T> to execute a function asynchronously.
- Use .Value to retrieve the result (blocks if not ready).
- Optionally use OnTerminated or Event to get notified.
uses
System.Threading, System.SysUtils;
procedure UseFuture;
var
FutureResult: IFuture<Int64>;
begin
// Start the task
FutureResult := TTask.Future<Int64>(
function: Int64
begin
Result := SlowFibonacci(40); // Heavy computation
end);
begin
UseFuture;
end.
Tip: Avoid blocking UI with .Value; instead use a Timer or Event system to check
IsCompleted.