FileReadToBufferThreadUnit.pas 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. {
  2. This Delphi unit is part of TIGCC.
  3. Copyright (C) 2000-2004 Sebastian Reichelt
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. }
  16. unit FileReadToBufferThreadUnit;
  17. interface
  18. uses
  19. SysUtils, Classes, Windows, Forms, Controls, SyncObjs;
  20. type
  21. TFileReadToBufferThread = class(TThread)
  22. private
  23. FHasTerminated: Boolean;
  24. FFileHandle: THandle;
  25. FBufferStream: TStream;
  26. FLock: TMultiReadExclusiveWriteSynchronizer;
  27. protected
  28. procedure Execute; override;
  29. public
  30. constructor Create(FileHandle: THandle; BufferStream: TStream);
  31. destructor Destroy; override;
  32. property HasTerminated: Boolean read FHasTerminated;
  33. property Lock: TMultiReadExclusiveWriteSynchronizer read FLock;
  34. end;
  35. implementation
  36. const
  37. BytesToRead = 256;
  38. { THandleWaitThread }
  39. constructor TFileReadToBufferThread.Create(FileHandle: HFile; BufferStream: TStream);
  40. begin
  41. inherited Create (True);
  42. FLock := TMultiReadExclusiveWriteSynchronizer.Create;
  43. FHasTerminated := False;
  44. FreeOnTerminate := False;
  45. FFileHandle := FileHandle;
  46. FBufferStream := BufferStream;
  47. Resume;
  48. end;
  49. destructor TFileReadToBufferThread.Destroy;
  50. begin
  51. FLock.Free;
  52. inherited;
  53. end;
  54. procedure TFileReadToBufferThread.Execute;
  55. var
  56. Buffer: array [1..BytesToRead] of Byte;
  57. Count: Cardinal;
  58. begin
  59. while (not Terminated) and ReadFile (FFileHandle, Buffer, BytesToRead, Count, nil) do begin
  60. Lock.BeginWrite;
  61. FBufferStream.Write (Buffer, Count);
  62. Lock.EndWrite;
  63. end;
  64. FHasTerminated := True;
  65. end;
  66. end.