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;
source
share