Delphi文件搜索多线程

6
如果我这样执行,我的应用程序将不会响应,直到找到所有文件并将它们添加到列表框中。我的问题是如何使此函数多线程化以避免无响应的情况!我仍然是Delphi新手。
procedure TfrMain.FileSearch(const PathName, FileName : string; txtToSearch : string; const InDir : boolean);
var Rec  : TSearchRec;
    Path : string;
    txt  : string;
    fh   : TextFile;
    i    : integer;
begin


Path := IncludeTrailingBackslash(PathName);
if FindFirst(Path + FileName, faAnyFile - faDirectory, Rec) = 0 then
 try
   repeat

     AssignFile(fh, Path + Rec.Name);
     Reset(fh);
     Readln(fh,txt);

     if ContainsStr(txt, txtToSearch) then
        ListBox1.Items.Add(Path + Rec.Name);

   until FindNext(Rec) <> 0;
 finally
   FindClose(Rec);

 end;

If not InDir then Exit;

if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then
 try
   repeat
    if ((Rec.Attr and faDirectory) <> 0)  and (Rec.Name<>'.') and (Rec.Name<>'..') then
     FileSearch(Path + Rec.Name, FileName, txtToSearch, True);
   until FindNext(Rec) <> 0;
 finally
   FindClose(Rec);
 end;
end;
2个回答

6
您可以将文件扫描的内容放入到一个线程中,在工作完成后向主窗体发送Windows消息,然后更新列表框(代码未经测试,仅供参考):
const 
  WM_FILESEARCH_FINISHED = WM_USER + 1;

TFileSearchThread = class (TThread)
private
  FPath       : String;
  FFileNames  : TStringList;
protected
  procedure Execute; override;
public
  constructor Create (const Path : String);
  destructor Destroy; override;
  property FileNames : TStrings read FFileNames;
end;

constructor TFileSearchThread.Create (const Path : String);
begin
  inherited Create (True);
  FPath := Path;
  FFileNames := TStringList.Create;
end;

destructor TFileSearchThread.Destroy;
begin
  FreeAndNil (FFileNames);
  inherited;
end;

procedure TFileSearchThread.Execute;  
begin
  // do your file search here, adding each file to FFileNames
  PostMessage (MainForm.Handle, WM_FILESEARCH_FINISHED, 0, 0);
end;

您可以像这样使用它:
Thead := TFileSearchThread.Create (Path);
Thread.Start;

主表单将会有一个类似这样的消息处理程序:

type
  TMainForm = class(TForm)
    ListBox1: TListBox;
  private
    procedure WMFileSearchFinished (var Msg : TMessage); message WM_FILESEARCH_FINISHED;
  public
    { Public declarations }
  end;

implementation

procedure TMainForm.WMFileSearchFinished (var Msg : TMessage);
begin
  ListBox1.Items.AddStrings (Thread.FileNames);
end;

PostMessage(MainForm.Handle, WM_FILESEARCH_FINISHED, 0, 0) 应该和 Synchronize 一起使用吗? - kobik
1
@kobik,不用担心,Handle属性是只读的,其读取是原子性的。而且它必须与0不同(控件将无法工作并且无法接收任何消息),因此getter永远不会在句柄为0时调用CreateHandle。风险在于对象实例本身,如果销毁了MainForm并尝试访问它,则通常会出现AV。而且,Emba uses也是这样做的 :-) - TLama
@kobik Synchronize 内部使用 SendMessage,它是 PostMessage 的阻塞等效版本。 - jpfollenius

5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接