Delphi - Indy TCP ReadStream Synchronize正在让应用程序停止响应

时间:2015-01-08 18:57:23

标签: delphi tcp indy

我正在尝试使用Indy IdTCPServer和IdTCPClient创建一个简单的测试应用程序来获取远程文件。它适用于轻量级流(少于1或2 MB),但当ReadStream完成下载大文件(例如10 MB)并将保存它时,应用程序停止响应几秒钟,我在应用程序上得到了那个丑陋的错误窗口标题(“没有响应”)。

聚苯乙烯。我知道在尝试保存流(文件)时会发生冻结,因为我使用进度条来了解下载进度。

以下是代码:

type
  TSyncStm = class(TIdSync)
  protected
    FStm: TMemoryStream;
    procedure DoSynchronize; override;
  public
    constructor Create; override;
    destructor Destroy; override;
  end;

constructor TSyncStm.Create;
begin
  inherited Create;
  FStm := TMemoryStream.Create;
end;

destructor TSyncStm.Destroy;
begin
  FreeAndNil(FStm);
  inherited
end;

procedure SyncStm(const Stm: TMemoryStream);
var
  Sync: TSyncStm;
begin
  if not Assigned(Stm) then Exit;
  Sync := TSyncStm.Create;
  try
    Sync.FStm.LoadFromStream(Stm);
    Sync.Synchronize;
  finally
    Sync.Free;
  end;
end;

procedure TSyncStm.DoSynchronize;
begin
  FStm.SaveToFile(ExtractFilePath(Application.ExeName) + \files\file01.zip');
  frmMain.FileListBox1.Update;
end;

procedure TfrmMain.TCPServerExecute(AContext: TIdContext);
var
  SMsg: string;
  Client: TClient;
  LQueue: TStringList;
  Stream: TMemoryStream;
begin
  ...
  // Here I have a working queue method, to process commands
  // when server sends a command 'sendfile' to client, it returns the file (stream)
  ...
  Stream := TMemoryStream.Create;
  try
    ReadStream(Stream, -1);
    if (Stream <> nil) then
    begin
      Stream.Position := 0;
      SyncStm(Stream);
    end;
  finally
    Stream.Free;
  end;
  ...
end;

请问,这是处理和同步流的最佳方式,而不会让应用程序停止响应?

0 个答案:

没有答案