I
I
Igor2019-11-14 23:24:25
Delphi
Igor, 2019-11-14 23:24:25

Delphi console application async call proc?

Greetings!
Console application.
Embarcadero® Delphi 10.3 Version 26.0.34749.6593
Created a test class, I want to make an asynchronous call to a certain piece of code.
Why do I need it?
I am planning to use this kind of structure

Http.Get(AUrl).BeforeRequest(
        procedure(AURL: string; Stream: TStream; Headers: TNetHeaders)
        begin
           // Перед запросом
        end).AfterRequest(
        procedure(ARatry: boolean; HTTPResponse: IHTTPResponse)
        var
          JsonObject: TJSONObject;
          csrfToken: string;
        begin
          JsonObject := TJSONObject.ParseJSONValue(HTTPResponse.ContentAsString(TEncoding.UTF8)) as TJSONObject;
          if JsonObject.TryGetValue('csrfToken', csrfToken) then
          begin
              // После выполнения запроса
             // ARatry = true - если нам нужно повторить запрос
            ARatry := true
          end;
        end).Success(
        procedure(Content: string)
        begin
           // Ответ сервера status code 200
        end);

Class to achieve the goal.
unit Http;

interface

uses
  System.SysUtils, System.Classes, System.Net.HttpClient, System.Net.URLClient,
  Winapi.Windows, syncobjs;

type
  THttp = class
    FBeforeRequestProc: TProc<string, TStream, TNetHeaders>;
    FAfterRequestProc: TProc<boolean, IHTTPResponse>;
    FSuccessProc: TProc<string>;
  protected
  public
    function BeforeRequest(Proc: TProc<string, TStream, TNetHeaders>): THttp;
    function AfterRequest(Proc: TProc<boolean, IHTTPResponse>): THttp;
    function Success(const Proc: TProc<string>): THttp;
    function Get(const AURL: string; const AResponseContent: TStream = nil; const AHeaders: TNetHeaders = nil): THttp;
  end;

implementation



{ THttp }

function THttp.AfterRequest(Proc: TProc<boolean, IHTTPResponse>): THttp;
begin
  FAfterRequestProc := Proc;
end;

function THttp.BeforeRequest(Proc: TProc<string, TStream, TNetHeaders>): THttp;
begin
  FBeforeRequestProc := Proc;
end;

function THttp.Get(const AURL: string; const AResponseContent: TStream; const AHeaders: TNetHeaders): THttp;
var
  Headers: TNetHeaders;
  Retry: boolean;
  Response: IHTTPResponse;
  THread: TTHread; 
begin
  Headers := AHeaders;
  THread := TTHread.CreateAnonymousThread(
    procedure
    var
     http: THttpClient;
    begin
      try
        http := THTTPClient.Create;
        FBeforeRequestProc(AURL, AResponseContent, AHeaders);
        Response := http.get(AURL, AResponseContent, AHeaders);
        FAfterRequestProc(Retry, Response);
        if Response.StatusCode = 200 then
          FSuccessProc(Response.ContentAsString(TEncoding.UTF8));
      finally
        FreeAndNil(http);
      end;
    end);
  THread.Resume;
end;

function THttp.Success(const Proc: TProc<string>): THttp;
begin
  FSuccessProc := Proc;
end;

end.

The problem is that when creating an http object of the THTTPClient class , an
access violation at address write of address error occurs
.
function THttp.Get(const AURL: string; const AResponseContent: TStream; const AHeaders: TNetHeaders): THttp;
var
  Headers: TNetHeaders;
  Retry: boolean;
  Response: IHTTPResponse;
  THread: TTHread; 
begin
  Headers := AHeaders;
  THread := TTHread.CreateAnonymousThread(
    procedure
    var
     http: THttpClient;
    begin
      try
        http := THTTPClient.Create;
        FBeforeRequestProc(AURL, AResponseContent, AHeaders);
        Response := http.get(AURL, AResponseContent, AHeaders);
        FAfterRequestProc(Retry, Response);
        if Response.StatusCode = 200 then
          FSuccessProc(Response.ContentAsString(TEncoding.UTF8));
      finally
        FreeAndNil(http);
      end;
    end);
  THread.Resume;
end;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Hemul GM, 2019-11-15
@IgorPI

5dce0376cd752331096656.png

I
Igor, 2019-11-14
@IgorPI

The problem is that the main thread terminates prematurely.
To do this, after
calling
THread.WaitFor;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question