简体   繁体   English

Delphi 10 Indy IdHttp 如何在Task中实现OnWork

[英]Delphi 10 Indy IdHttp how to implement OnWork in Task

I implemented a TTask to perform multiple uploads.我实现了一个 TTask 来执行多次上传。 I still have to implement IdHttp's OnWorkBegin, OnWork, OnWorkEnd methods in the task I created but I don't know how.我仍然需要在我创建的任务中实现 IdHttp 的 OnWorkBegin、OnWork、OnWorkEnd 方法,但我不知道如何实现。

var TASK: ITask;


begin

  TASK := TTask.Create(
      procedure
      var
        IdSSL: TIdSSLIOHandlerSocketOpenSSL;
        lParam : TIdMultipartFormDataStream;
        UrlAPI: string;
        res: string;
        lHTTP: TIdHTTP;
      begin

        UrlAPI := 'https://..........';
        lHTTP := TIdHTTP.Create(nil);
    //I want to handle the OnWork methods here but I don't know where to declare them with this code structure that I would like to keep.
        //lhttp.OnWorkBegin:= IdHTTPOnWorkBegin;
        //lhttp.OnWork:=IdHTTP1Work;
        //lhttp.OnWorkEnd:=IdHTTPOnWorkEnd;
        TThread.Synchronize(nil,
          procedure
          begin
            Memo1.Lines.Add('Task Running...');
          end
        );
          lHTTP.ReadTimeout := 30000;
          lHTTP.HandleRedirects := false;
          lParam := TIdMultipartFormDataStream.Create;
          lParam.AddFormField('param1', code1);
          lParam.AddFormField('param2', code2);
          lParam.AddFile('source', TheFile);
          lParam.Position := 0;
          try
            res := lHTTP.Post(UrlAPI, lparam);
            memo1.Lines.Add(risposta);
          Finally
            lHTTP.Free;
          end;
          TThread.Synchronize(nil,
            procedure
            begin
              Memo1.Lines.Add('SEND file '+TheFile);
             end
          );
      end
    );
    TASK.Start();

Where do I write the declarations of the methods so that they can include the declaration of the IHTTP?我在哪里编写方法的声明,以便它们可以包含 IHTTP 的声明?

The same way you always do it in Delphi.与您在 Delphi 中总是这样做的方式相同。

type
  TForm1 = class(TForm)
    procedure IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode;
      AWorkCount: Int64);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  System.Threading;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  Task: ITask;
begin

  Task := TTask.Create(
  procedure
  var
    IdHttp1: TIdHttp;
  begin
    IdHttp1 := TidHttp.Create(Self);
    IdHttp1.OnWork := Form1.IdHTTP1Work;
  end
  );
  Task.Start; 

end;

procedure TForm1.IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode;
  AWorkCount: Int64);
var
  IdHttp: TIdHttp;
begin
  IdHttp := ASender as TIdHttp;
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM