Forum > General

(solved) How to show progres with TReader

(1/2) > >>

Imants:
I am reading large file and want to show reading progress. The problem is I don't know in witch position now reader is in stream;

It is something like this:

--- Code: ---      try
      FStream := TFileStream.Create(UTF8toAnsi(ADirectory + FFiles.Name), fmOpenRead);
      FReader := TReader.Create(FStream, $FF);
      try
        ProgressBar.Max := FileSize(UTF8toAnsi(ADirectory + FFiles.Name));
        ProgressBar.Position := 0;
        LoadClass(FReader, ProgressBar); // loading data
        .....
      finally
        FReader.Free;
        FStream.Free
      end;

Procedure LoadClass(Reader: TReader; FProgress: Tprogressbar);
Begin
  while not Done do
  Begin
     //Do some reading
    FProgress.Position := ?
  end
end;  

--- End code ---

howardpc:
I'm not sure why you need both a TFileStream and a TReader.
Why not use just a filestream and set


--- Code: ---ProgressBar.Position := FStream.Position;
--- End code ---

H

Imants:

--- Quote from: howardpc on December 21, 2010, 11:14:50 pm ---I'm not sure why you need both a TFileStream and a TReader.
Why not use just a filestream and set


--- Code: ---ProgressBar.Position := FStream.Position;
--- End code ---

H

--- End quote ---

I need both because TReader needs TStream in his constructor. And I use TReader because ite is very convenient for me and I don't see any reason to not use it.

Regarding my problem I solved it by creating custom reader:


--- Code: ---  TProgressReader = class(TReader)
  private
    FStream: TStream;
    FProgresBar: TProgressBar;
    function GetPosition: Integer;
  public
    constructor Create(Stream: TStream; BufSize: Integer; AProgressBar: TProgressBar);

    Property Position: Integer read GetPosition;
    Procedure ShowProgress;
  end;  

function TProgressReader.GetPosition: Integer;
begin
  Result := FStream.Position;
end;

constructor TProgressReader.Create(Stream: TStream; BufSize: Integer;
  AProgressBar: TProgressBar);
begin
  inherited Create(Stream, BufSize);
  FProgresBar := AProgressBar;
  FStream := Stream;
end;

procedure TProgressReader.ShowProgress;
begin
  FProgresBar.Position := Position
end;  

--- End code ---

captian jaster:

--- Quote from: Imants on December 22, 2010, 09:56:02 am ---
--- Quote from: howardpc on December 21, 2010, 11:14:50 pm ---I'm not sure why you need both a TFileStream and a TReader.
Why not use just a filestream and set


--- Code: ---ProgressBar.Position := FStream.Position;
--- End code ---

H

--- End quote ---

I need both because TReader needs TStream in his constructor. And I use TReader because ite is very convenient for me and I don't see any reason to not use it.

Regarding my problem I solved it by creating custom reader:


--- Code: ---  TProgressReader = class(TReader)
  private
    FStream: TStream;
    FProgresBar: TProgressBar;
    function GetPosition: Integer;
  public
    constructor Create(Stream: TStream; BufSize: Integer; AProgressBar: TProgressBar);

    Property Position: Integer read GetPosition;
    Procedure ShowProgress;
  end;  

function TProgressReader.GetPosition: Integer;
begin
  Result := FStream.Position;
end;

constructor TProgressReader.Create(Stream: TStream; BufSize: Integer;
  AProgressBar: TProgressBar);
begin
  inherited Create(Stream, BufSize);
  FProgresBar := AProgressBar;
  FStream := Stream;
end;

procedure TProgressReader.ShowProgress;
begin
  FProgresBar.Position := Position
end;  

--- End code ---

--- End quote ---
I still never understood why the treader\twriter was made, when the memory and file streams all had the reading features..

Imants:

I still never understood why the treader\twriter was made, when the memory and file streams all had the reading features..
[/quote]

Reader/writer have easier writing reading functions like readstring, readinteger, readfloat...
and streams only had Read write function which is little harder to use.

Navigation

[0] Message Index

[#] Next page

Go to full version