delphi file search multithreading delphi file search multithreading multithreading multithreading

delphi file search multithreading


Here you can find an article about the background file scanner implemented with OmniThreadLibrary.


You can put the file scanning stuff into a thread and whenver work is finished send a windows message to the main form, which then updates the list box (code not tested, take it as pseudo code):

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;

You could use it like this:

Thead := TFileSearchThread.Create (Path);Thread.Start;

and the main form would have a message handler like this:

type  TMainForm = class(TForm)    ListBox1: TListBox;  private    procedure WMFileSearchFinished (var Msg : TMessage); message WM_FILESEARCH_FINISHED;  public    { Public declarations }  end;implementationprocedure TMainForm.WMFileSearchFinished (var Msg : TMessage);begin  ListBox1.Items.AddStrings (Thread.FileNames);end;