2

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

4

2 回答 2

7

这里有一个简单的例子,说明如何子类化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事件是在进程的文件端实现的,但它所代表的实际进度是整个进程,包括数据库通信。

于 2013-02-05T18:48:21.597 回答
1

我认为延迟不是为了读取文件,而是为了数据库中的记录。我相信您可以根据文件大小和使用“TTimer”的项目进度进行估计,但是对于这个读取文件应该在一个线程中完成,以便应用程序的其余部分(主线程)锁定

于 2013-02-05T12:10:49.527 回答