从您的代码中很明显,当您显示警报时,文件传输已经发生:这只是“我是否保存到文件”或“我是否丢弃我已经收到的内容”的问题。我从您对 - 该函数的使用中推断出此信息TMemoryStream.Write()- 该函数将缓冲区作为参数,因此我假设Content[1]为您提供了缓冲区。这也意味着Content已经填充了您需要的数据。不转移它已经太晚了,它已经在内存中,你所能做的就是将它保存到磁盘或丢弃它。
我也不知道 TMS 的警报是如何工作的,但我会假设在任何给定时间只能显示一个警报,并且我会假设您将您Alert的警报放在一个组件上(即:只有一个警报在整个程序)。
您应该首先更改“接收到的事件”的代码,以立即将内容移动到TMemoryStream. 还要确保您不会遇到递归重入的麻烦。在表单中添加一个私有字段,调用它FLastContentReceived: TMemoryStream;现在将您的代码更改为如下所示:
procedure TfrmReadFile.ServerReceiveEvent(Sender: TObject;
Client: TSimpleTCPClient; Event: TTOOCSEvent);
begin
if (Event is TTOOCSEventFileTransfert) then
begin
// Re-entry before we managed to handle the previous received file?
if Assigned(FLastContentReceived) then
raise Exception.Create('Recursive re-entry not supported.');
// No re-entry, let's save the content we received so we can save it to file
// if the user clicks the Alert button.
FLastContentReceived := TMemoryStream.Create;
// I don't know what Content is, but you've got that in your code so I
// assume this will work:
FLastContentReceived.Write(Content[1], Length(Content);
// Show the alert; If the OnAlertClick event fires we'll have the received file content
// in the FLastContentRecevied and we'll use that to save the file.
Alert.Show;
end;
end;
您正在尝试执行 if on Alert.OnAlertClick- 所以我假设您的Alert组件中有一个名为OnAlertClick. 把它写在它的事件处理程序中:
procedure TfrmReadFile.AlertAlertClick(Sender: TObject);
begin
if not Assigned(FLastContentReceived) then raise Exception.Create('Bug');
try
if dlgSaveFile.Execute then
FLastContentReceived.SaveToFile(dlgSaveFile.FileName);
finally FreeAndNil(FLastContentReceived);
end;
end;
您还需要一种方法来丢弃FLastContentReceived如果在单击警报按钮之前关闭表单或出现超时(警报在用户单击它的情况下消失)。表单关闭时的第一项工作(摆脱 FLastContentReceived)很简单:将其添加到表单的OnDestroy:
FLastContentRecevid;Free;
处理超时可能有点困难。如果Alert有一个在警报超时时调用的事件并且气球在没有被单击的情况下消失,则使用该事件处理程序来执行此操作:
FreeAndNil(FLastContentRecevid);
如果它没有提供这样的东西,您可以设置一个TTimer等于警报超时的时间间隔(或稍长一些以确保安全),在显示警报之前启用它并从它执行此操作OnTimer:
procedure TFrmReadFile.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
FreeAndNil(FLastContentRecevid);
end;