How it works...

Open the project AsyncTaskSample.dproj, and then let's talk about the AsyncTask.pas unit.

Here's the unit with some comments:

unit AsyncTask; 
  System.Threading; //The PPL unit 
  //the "background" task 
  TAsyncBackgroundTask<T> = reference to function: T; 
  //the "success" callback 
  TAsyncSuccessCallback<T> = reference to procedure(const TaskResult: T); 
  //the "error" callback 
  TAsyncErrorCallback = reference to procedure(const E: Exception); 
  //the default "error" callback if the user does not provide it  
  TAsyncDefaultErrorCallback = reference to procedure(const E: Exception;     const ExptAddress: Pointer); 
  //the main class  
  Async = class sealed  
    class function Run<T>(Task: TAsyncBackgroundTask<T>; ...

Get Delphi Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.