Delphi Threading with TRestRequest

I am relatively new to RestRequest tools. I hope someone solves the issue of waiting for the completion of the asynchronous call ...

I have a program that makes hundreds of different api calls, the call then processes the result and passes the result back to the procedure that caused the api to execute, which then can continue ...

The only problem with such calls without streaming is that the software freezes until the call is completed ... To try and fix this, I changed RESTRequest.Execute;to RESTRequest.ExecuteAsync();, but now my problem is that my code continues, without waiting for an answer to everything else.

again, to try to get around this problem, I tried several solutions, even api.RESTRequest.ExecuteAsync().WaitFor;(which causes the error thread error: the handler is invalid (6))

Is there any way that I can change the function below to work as a separate thread (only part of the execution is important for running in the thread) ... Basically, I just want to show an animated loading icon every time the function is called, and for the rest of it of my code - until this function completes ...

Hopefully there is a simpler solution than starting to use full for multithreading.

code

function run_api_command():boolean;
begin

  result := false;
  RESTResponse.Content.Empty;
  RESTAdapter.Dataset:= ds_action;
  RESTAdapter.RootElement:= '';

  try
    RESTRequest.ExecuteAsync;

    if(ds_action.Active = false) then ds_action.Active:=true;
    if(ds_action.FieldByName('result').AsString<>'Success') then
      begin
        showmessage(ds_action.FieldByName('result').AsString);
      end
    else
      begin
        RESTAdapter.RootElement:= 'data';
        result := true;
      end;
  except
    on E: Exception do
      begin
        if(e.Message = 'REST request failed: Error sending data: (12007) The server name or address could not be resolved') then
          begin
            if(messagedlg('Could not connect to server. Would you like to retry?', mterror,[mbYes,mbNo],0)=mrYes) then
              begin
                result := run_api_command();
              end;
          end
        else
          begin
            showmessage(RESTResponse.Content);
          end;
      end;
  end;
end;
+6
source share
2 answers

ExecuteAsync() ( TRESTExecutionThread, ). ExecuteAsync() AFreeThread, True. :

AFreeThread False, .

. AFreeThread True, .

WaitFor() .

, AFreeThread=True, WaitFor() . TThread object FreeOnTerminate True, API-, , WaitFor() " ". TThread.WaitFor() 1 , TThread.FreeOnTerminate=True.

( 1 Delphi 6, TThread Kylix, ​​ .)

, REST, :

  • Execute() ExecuteAsync() AFreeThread=False, WaitFor() ( , MsgWaitForMultipleObjects()) , :

  • Execute(), .

- , . ExecuteAsync(), , , , . ExecuteAsync, .

procedure run_api_command();
begin
  ...
  RESTRequest.ExecuteAsync(
    procedure
    begin
      if not RESTResponse.Status.Success then
      begin
        // do something ...
      end else begin
        // do something else ...
      end;
    end,
    True
  );
end;
+7

...

", , , restrequest"

Async. , - . , , , .

- , , . Execute ExecuteAsync, . Execute . , ExecuteAsync , , . Async - .

- . , . , .

"... " api ", ..."

. , . , . OnClick . , ( ), . , , - , .

, WaitFor . . WaitFor . - "", , -.


. . , "" , . , . , , , .


, ...

i hate programming

+5

All Articles