进度条到loadfromfile函数

时间:2013-02-05 10:57:32

标签: delphi progress-bar blob

我使用Delphi LoadFromFile函数插入Table1BLOBFIELD.LoadFromFile ('C:\ xxx.avi')和要检索的SaveToFile命令。 这种插入/检索相当耗费时间(几分钟)。它 如果我可以显示一种进度条(而不仅仅是一个),那将会很好 沙漏形光标)。 使用SaveToFile时,我可以使用计时器并检查文件大小 (虽然没有线程似乎没有用)。但这不是 LoadFromFile的解决方案。 有没有人有特定的解决方案,暗示,......? 提前致谢

2 个答案:

答案 0 :(得分: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事件是在流程的文件端实现的,但它所代表的实际进度是整个流程,包括数据库通信。

答案 1 :(得分:1)

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