Recent

Author Topic: ReadProgressStream and WriteProgressStream  (Read 234 times)

Tomxe

  • Full Member
  • ***
  • Posts: 102
ReadProgressStream and WriteProgressStream
« on: May 23, 2026, 09:23:49 am »
Wherever TStream is used, you can easily attach this class to monitor the progress of data reading/writing.

https://github.com/Xelitan/Various-Free-Pascal-TStreams

Code: Pascal  [Select][+][-]
  1. var
  2.   fs: TFileStream;
  3.   ws: TWriteProgressStream;
  4.  
  5. procedure OnProgress(BytesWritten, TotalBytes: Int64);
  6. begin
  7.   ProgressBar.Position := Round(BytesWritten / TotalBytes * 100);
  8. end;
  9.  
  10. begin
  11.   fs := TFileStream.Create('out.bin', fmCreate);
  12.   ws := TWriteProgressStream.Create(fs, UncompressedSize);
  13.   ws.OnProgress := OnProgress;
  14.  
  15.   SomeDecompressor.WriteTo(ws);
  16.  
  17.   ws.Free;
  18.   fs.Free;
  19. end;

Xenno

  • Full Member
  • ***
  • Posts: 109
    • BS Programs
Re: ReadProgressStream and WriteProgressStream
« Reply #1 on: May 23, 2026, 01:58:32 pm »
Nice trick!

However, because these classes inherit from TStream but hold a private TStream field internally, these read/write classes themselves seem to have potential issues. While modifying each stream class directly would be the proper approach—since each has its own implementation—doing so might introduce larger regressions.

What is the actual use case for this event? Wouldn't we get the same result by simply checking the stream's Position property inside a loop? The loop will be required in order to track progress.
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

Tomxe

  • Full Member
  • ***
  • Posts: 102
Re: ReadProgressStream and WriteProgressStream
« Reply #2 on: May 24, 2026, 11:39:56 am »
Yes, you can use a loop, sometimes. Some compressors/ decompressors don't allow you to loop and write/read data to it, instead they just ask for a TStream and do the work on it.

 

TinyPortal © 2005-2018