我对RestRequest工具相对陌生。希望有人解决了等待异步调用完成的问题...
我有一个程序,它进行了数百次不同的API调用,然后处理结果,并将结果传回调用API执行的过程,然后可以继续执行...
唯一的问题总是在没有线程的情况下进行这种调用时,软件会挂起,直到调用完成... 为了解决这个问题,我将RESTRequest.Execute;
改为RESTRequest.ExecuteAsync();
,但现在我的问题是我的代码继续执行而不等待REST请求的响应。
为了尝试解决这个问题,我已经尝试了几种解决方案,甚至使用了api.RESTRequest.ExecuteAsync().WaitFor;
(抛出一个错误thread error: the handler is invalid (6)
)
是否有任何方法可以将下面的函数更改为在单独的线程中运行(只有执行部分重要运行在线程中)... 基本上,我只想在每次调用该函数时显示一个动画加载图标,并且让我的其余代码等待此函数完成...
我希望有一个比开始使用完全多线程更简单的解决方案..
代码
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;