Delphi TaskCompleted Event (using the Chilkat ActiveX)

Demonstrates the TaskCompleted event callback using the Chilkat Delphi ActiveX:

procedure TForm1.httpProgressInfo(ASender: TObject;  const name: WideString; const value: WideString);

begin
    // This event callback occurs in the background thread (because the asynchronous
    // version of the QuickGetStr method was called (i.e. QuickGetStrAsync)
    // UI elements must be updated from the main UI thread...
    TThread.Synchronize(nil, procedure begin
      Memo1.Lines.Add(name + ': ' + value);
      end);

end;

procedure TForm1.httpTaskCompleted(ASender: TObject;  const task: IChilkatTask);
begin
    // This event callback occurs in the background thread (because the asynchronous
    // version of the QuickGetStr method was called (i.e. QuickGetStrAsync)
    // UI elements must be updated from the main UI thread...
    TThread.Synchronize(nil, procedure begin
      Memo1.Lines.Add(task.ResultErrorText);
      end);

end;

procedure TForm1.Button2Click(Sender: TObject);
var
  http: TChilkatHttp;
  success: Integer;
  html: WideString;
  task: IChilkatTask;

begin
  http := TChilkatHttp.Create(Self);

  success := http.UnlockComponent('anything for 30-day trial');
  if (success <> 1) then
  begin
    Memo1.Lines.Add(http.LastErrorText);
    Exit;
  end;

  http.OnTaskCompleted := httpTaskCompleted;
  http.OnProgressInfo := httpProgressInfo;

  // Create a task to do the HTTP GET asynchronously.
  task := http.QuickGetStrAsync('http://www.chilkatsoft.com/');

  // Start the task in a background thread.
  task.Run();

  // Warning: If an event callback method calls TThread.Synchronize, then a call to task.Wait will hang
  // for the full duration of the timeout specified.  (If the wait-forever value of 0 is passed
  // to task.Wait, then it will hang forever.) This is because the call to TThread.Synchronize is waiting
  // for the main thread's event loop.  Control is not returned to the main event loop until this
  // TForm1.Button2Click returns -- thus there is a deadlock. There is no deadlock when a program
  // Waits on a task and there are no event callbacks that call TTHread.Synchronize.
  //task.Wait(5000);

end;