gpt4 book ai didi

delphi - loadfromfile 函数的进度条

转载 作者:行者123 更新时间:2023-12-02 05:12:37 26 4
gpt4 key购买 nike

我使用Delphi LoadFromFile函数插入Table1BLOBFIELD.LoadFromFile('C:\xxx.avi') 和 SaveToFile 命令来检索。这种插入/检索相当耗时(几分钟)。它如果我可以显示一种进度条(而不仅仅是一个进度条),那就太好了沙漏形光标)。使用 SaveToFile 时,我也许可以使用计时器并检查文件大小(尽管这似乎在没有线程的情况下不起作用)。但这不是LoadFromFile 的解决方案。有谁有特定的解决方案、提示……?提前致谢

最佳答案

这里是一个简单的示例,说明如何子类化 TFileStream 以及如何向其添加 OnProgress 事件:

unit Unit1;

interface

uses
SysUtils, Classes, Controls, Forms, XPMan, Db, ComCtrls, StdCtrls, AdoDB;

type
TStreamProgressEvent = procedure(Sender: TObject;
Percentage: Single) of object;

TProgressFileStream = class(TFileStream)
private
FOnProgress: TStreamProgressEvent;
FProcessed: Int64;
FSize: Int64;
public
procedure InitProgressCounter(ASize: Int64);
function Read(var Buffer; Count: Integer): Integer; override;
function Write(const Buffer; Count: Integer): Integer; override;
property OnProgress: TStreamProgressEvent read FOnProgress
write FOnProgress;
end;

TForm1 = class(TForm)
ADOQuery1: TADOQuery;
ADOQuery1ID: TAutoIncField;
ADOQuery1Blob: TBlobField;
Button1: TButton;
ProgressBar: TProgressBar;
procedure Button1Click(Sender: TObject);
private
procedure StreamProgress(Sender: TObject; Percentage: Single);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TProgressFileStream }

procedure TProgressFileStream.InitProgressCounter(ASize: Int64);
begin
FProcessed := 0;
if ASize <= 0 then
FSize := 1
else
FSize := ASize;
if Assigned(FOnProgress) then
FOnProgress(Self, 0);
end;

function TProgressFileStream.Read(var Buffer; Count: Integer): Integer;
begin
Result := inherited Read(Buffer, Count);
Inc(FProcessed, Result);
if Assigned(FOnProgress) then
FOnProgress(Self, FProcessed / FSize);
end;

function TProgressFileStream.Write(const Buffer; Count: Integer): Integer;
begin
Result := inherited Write(Buffer, Count);
Inc(FProcessed, Result);
if Assigned(FOnProgress) then
FOnProgress(Self, FProcessed / FSize);
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
Stream: TProgressFileStream;
begin
ADOQuery1.Open;
Stream := TProgressFileStream.Create('H:\Delphi\Blobtest.docx', fmOpenRead);
try
Stream.OnProgress := StreamProgress;
Stream.InitProgressCounter(Stream.Size);
ADOQuery1.Append;
ADOQuery1Blob.LoadFromStream(Stream);
ADOQuery1.Post;
finally
Stream.Free;
end;
Stream := TProgressFileStream.Create('H:\Delphi\Blobtest2.docx', fmCreate);
try
ADOQuery1.Last;
Stream.OnProgress := StreamProgress;
Stream.InitProgressCounter(ADOQuery1Blob.BlobSize);
ADOQuery1Blob.SaveToStream(Stream);
finally
Stream.Free;
end;
end;

procedure TForm1.StreamProgress(Sender: TObject; Percentage: Single);
begin
ProgressBar.Position := Round(Percentage * ProgressBar.Max);
end;

end.

虽然这个OnProgress事件是在进程的文件端实现的,但它代表的实际进度是整个进程,包括数据库通信。

关于delphi - loadfromfile 函数的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14705871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com