Delphi DLL TaskCompleted Callbacks

The Chilkat Delphi DLL (non-ActiveX) supports callbacks starting in version 9.5.0.82, to be released in Feb 2020. Pre-release Beta builds are available upon request.

Also see:

1) First define a procedure exactly as shown here. Make sure to use the “cdecl” calling convention.

// Called from a background thread.
procedure MyTaskCompleted(task: HCkTask) cdecl;
var
    rest: HCkRest;
begin
    // Chilkat v9.5.0.82 introduces a new LoadTaskCaller method to get the instance of the object 
    // that made the async method call.
    // To do it, we first create a new/empty object of the correct type.  In this case, it is a Rest object.
    rest := CkRest_Create();

    // Next, we call LoadTaskCaller to make it reference the object instance that made the async call.
    CkRest_LoadTaskCaller(rest,task);

    // Now that we have the caller object, we can get its properties, such as the ResponseHeader:
    Form1.Memo1.Lines.Add(CkRest__responseHeader(rest));
    Form1.Memo1.Lines.Add('---');

    // The async method's return value is obtained from the Task object.  If the async method returns a string,
    // then we call CkTask__getResultString. 
    // (In the code below, you can see that we called CkRest_FullRequestNoBodyAsync.  
    // The FullRequestNoBody method returns a string, and therefore we can
    // get the string return value by calling CkTask__getResultString.)
    Form1.Memo1.Lines.Add(CkTask__getResultString(task));

end;

2) Set the task completed callback by calling the appropriate SetTaskCompleted function. For the CkRest object it is CkZip_SetTaskCompleted(rest,MyTaskCompleted);

Note: This example shows TaskCompleted callbacks using CkRest. The same technique applies to any Chilkat class having callbacks.
For example, CkHttp_SetTaskCompleted.

procedure TForm1.Button1Click(Sender: TObject);
var
rest: HCkRest;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
success: Boolean;
task: HCkTask;

begin
rest := CkRest_Create();

// Connect to the REST server.
bTls := True;
port := 443;
bAutoReconnect := True;
success := CkRest_Connect(rest,'www.alphavantage.co',port,bTls,bAutoReconnect);
if (success = False) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

success := CkRest_AddQueryParam(rest,'function','TIME_SERIES_DAILY');
success := CkRest_AddQueryParam(rest,'symbol','AAPL');
// You can test this code by getting a free API key at www.alphavantage.co
success := CkRest_AddQueryParam(rest,'apikey','my_api_key');

// Setup the TaskCompleted callback.
CkRest_SetTaskCompleted(rest,MyTaskCompleted);

// Create a task to send a GET request to https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=my_api_key
task := CkRest_FullRequestNoBodyAsync(rest,'GET','/query');
if (CkRest_getLastMethodSuccess(rest) = False) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

// Schedule the task for running on Chilkat's background thread pool.  This changes the task's state
// from Inert to Live.
success := CkTask_Run(task);
if (success = False) then
  begin
    Memo1.Lines.Add(CkTask__lastErrorText(task));
    CkTask_Dispose(task);
    Exit;
  end;

// The application is now free to do anything else
// while the HTTP request is in progress.  When the task
// is completed, the TaskCompleted callback is called.
CkTask_Dispose(task);

CkRest_Dispose(rest);

end;