Using TTask.IFuture from the Parallel Programming Library

From RAD Studio
Jump to: navigation, search

Go Up to Using the Parallel Programming Library

Future objects are objects that implement the IFuture interface, which defines a mechanism to access the result of a function asynchronously.

To create and use a future object:

  1. Call TTask.Future<T> to create a future object. The specified function starts running in the background.
  2. Read the Value property of the future object at any time to retrieve the result of the specified function. If the function execution in the background has not finished yet, Value blocks the program execution until the function finishes.

For example:


FutureObject := TTask.Future<Integer>(function: Integer
        Result := 16;
// …
MyValue := FutureObject.Value;


If you are using a Clang-enhanced C++ compiler, you can use lambda expressions:

auto FutureObject = TTask::Future<int>([&]()-> int {
    return 16;
// …
int MyValue = FutureObject->Value;

For any C++ compiler, you can declare a TProc subclass and use an instance of it casted as _di_TProc:

class TCppTask : public TCppInterfacedObject<TFunc<int> > {
    int __fastcall Invoke() {
        return 16;
// …
System::DelphiInterface<IFuture__1<int> > FutureObject = TTask::Future<int>(new TCppTask());
// …
int MyValue = FutureObject->Value;

See Also