简体   繁体   English

Delphi FireMonkey Indy TIdHttp 如何中止?

[英]Delphi FireMonkey Indy TIdHttp how to abort?

I am using Delphi 10.1 Berlin update and Indy's TIdHttp component.我正在使用 Delphi 10.1 Berlin 更新和 Indy 的 TIdHttp 组件。 I usually abort a connection using the following code:我通常使用以下代码中止连接:

procedure TMyThread.OnWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: int64);
begin
    if abort then (asender as tidhttp).Disconnect;
end;

This works fine in a VCL application, also in a FireMonkey application as long the platform is Windows.只要平台是 Windows,这在 VCL 应用程序和 FireMonkey 应用程序中都可以正常工作。 When I use this code with MacOS as target, it close my application instantly without error message or anything displayed.当我将此代码与 MacOS 一起用作目标时,它会立即关闭我的应用程序,而不会显示错误消息或任何内容。

So my question here is what am I doing wrong, and how can I abort a connection when the platform is MacOS?所以我的问题是我做错了什么,当平台是 MacOS 时如何中止连接?

Rather then call Disconnect() directly and rip the socket connection away from TIdHTTP , I would raise an exception instead, like calling SysUtils.Abort() , and let TIdHTTP or the calling thread close the socket after the request has been fully aborted. 而不是直接调用Disconnect()并使套接字连接脱离TIdHTTP ,而是引发异常,例如调用SysUtils.Abort() ,并在请求被完全终止后让TIdHTTP或调用线程关闭套接字。

procedure TMyThread.OnWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: int64);
begin
  if Self.Abort then SysUtils.Abort;
end;

procedure TMyThread.Execute;
var
  HTTP: TIdHTTP;
begin
  HTTP := TIdHTTP.Create;
  try
    ...
  finally
    HTTP.Free;
  end;
end;

try like this : 尝试这样:

procedure TMyThread.OnWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: int64);
begin
  try  
    if abort then (asender as tidhttp).Disconnect;
  except
  end;
end;
procedure TMyThread.Execute;
var
  HTTP: TIdHTTP;
begin
  HTTP := TIdHTTP.Create;
  try
    try
     ...
    except
      on E:Exception do ...//do not re-raise it.
    end
  finally
    HTTP.Free;
  end;
end;

Try catching the thread exception.尝试捕获线程异常。

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

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