Advertisement
Borrisholt

TComThread

Sep 16th, 2019
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.35 KB | None | 0 0
  1. unit FileScannerU;
  2.  
  3. interface
  4.  
  5. uses
  6.   System.Classes, System.Generics.Collections, FileCheckerU;
  7.  
  8. type
  9.   TMessageProc<T> = reference to procedure(const Data: T);
  10.  
  11.   TComThread<T> = class(TThread)
  12.   private
  13.     FReceiver: TMessageProc<T>;
  14.   strict protected
  15.     procedure SendToMain(const aValue: T);
  16.   public
  17.     constructor Create(aCreateSuspended: Boolean; const aReceiver: TMessageProc<T>); reintroduce;
  18.   end;
  19.  
  20.   TFileScanner = class(TComThread<TFileInformation>)
  21.   private
  22.     FPath: string;
  23.   protected
  24.     procedure Execute; override;
  25.   public
  26.     constructor Create(aPath: string; const aFileFoundProc: TMessageProc<TFileInformation>); reintroduce;
  27.     destructor Destroy; override;
  28.   end;
  29.  
  30. implementation
  31.  
  32. uses
  33.   System.SysUtils, System.IoUtils,
  34.  
  35.   FileUtilsU;
  36.  
  37. { TFileScanner }
  38.  
  39. constructor TFileScanner.Create(aPath: string; const aFileFoundProc: TMessageProc<TFileInformation>);
  40. begin
  41.   inherited Create(False, aFileFoundProc);
  42.   FreeOnTerminate := True;
  43.   FPath := aPath;
  44. end;
  45.  
  46. destructor TFileScanner.Destroy;
  47. begin
  48.   SendToMain(TFileInformation.Create(''));
  49.   inherited;
  50. end;
  51.  
  52. procedure TFileScanner.Execute;
  53. var
  54.   LastPath: string;
  55.   FileInformation: TFileInformation;
  56. begin
  57.   LastPath := '';
  58.  
  59.   if TFile.Exists(FPath) then
  60.   begin
  61.     if FileIsInteresting(FPath, FileInformation) then
  62.       SendToMain(FileInformation);
  63.     exit;
  64.   end;
  65.  
  66.   TDirectory.GetFiles(FPath, TSearchOption.soAllDirectories,
  67.     function(const Path: string; const SearchRec: TSearchRec): Boolean
  68.     begin
  69.       Result := False;
  70.  
  71.       if Terminated then
  72.         exit;
  73.  
  74.       if Path <> LastPath then
  75.       begin
  76.         LastPath := Path;
  77.         SendToMain(TFileInformation.Create(Path));
  78.       end;
  79.  
  80.       if FileUtils.IsValidExtention(TPath.GetExtension(SearchRec.Name)) then
  81.         if FileIsInteresting(TPath.Combine(Path, SearchRec.Name), FileInformation) then
  82.           SendToMain(FileInformation);
  83.     end);
  84. end;
  85.  
  86. { TComThread<T> }
  87.  
  88. constructor TComThread<T>.Create(aCreateSuspended: Boolean; const aReceiver: TMessageProc<T>);
  89. begin
  90.   FReceiver := aReceiver;
  91.   inherited Create(aCreateSuspended);
  92. end;
  93.  
  94. procedure TComThread<T>.SendToMain(const aValue: T);
  95. var
  96.   CallBack: TThreadProcedure;
  97. begin
  98.   CallBack := procedure
  99.     begin
  100.       FReceiver(aValue);
  101.     end;
  102.  
  103.   Synchronize(CallBack);
  104. end;
  105.  
  106. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement