HTTP Get using TTask

 
This tutorial shows how to use the parallel threads provided by the TTask class in the Http.Get example.
 
The code utilizes the Clever Internet Suite library.
 
 
procedure TForm1.Button1Click(Sender: TObject);
var
  task: ITask;
begin
  FAccessor.Enter();
  try
    if (FWorkInProgress) then Exit;
    FWorkInProgress := true;
  finally
    FAccessor.Leave();
  end;

  FNeedShowText := cbShowText.Checked;
  task := TTask.Create(procedure()
var
  html: TStrings;
begin
  html := TStringList.Create();
  try
    TThread.Synchronize(nil, procedure()
    begin
      Memo1.Lines.Clear();
    end);
    clHttp1.Get(edtUrl.Text, html);

    if FNeedShowText then
    begin
      clHtmlParser1.Parse(html);
    end else
    begin
      TThread.Synchronize(nil, procedure()
      begin
        Memo1.Lines.Assign(html);
      end);
    end;

  finally
    FAccessor.Enter();
    try
      FWorkInProgress := False;
    finally
      FAccessor.Leave();
    end;
    html.Free();
  end;
end);
  task.Start();
end;
 
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
 FAccessor.Enter();
  try
    if (FWorkInProgress) then
    begin
      ShowMessage('The work is in progress, cannot close the program.');
      CanClose := False;
    end;
  finally
    FAccessor.Leave();
  end;
end;

procedure TForm1.clHtmlParser1ParseTag(Sender: TObject; ATag: TclHtmlTag);
begin
  if (Trim(ATag.Text) <> '') then
  TThread.Synchronize(nil, procedure()
  begin
    Memo1.Lines.Add(ATag.Text);
  end);
end;
 
Have questions?
Join us Facebook   YouTube   Twitter   Telegram   Newsletter
 
Kind regards
Clever Components team
www.CleverComponents.com

Add Feedback