Delphi - threads and FindFirst function

Posted by radu-barbu on Stack Overflow See other posts from Stack Overflow or by radu-barbu
Published on 2010-06-18T15:53:44Z Indexed on 2010/06/18 16:03 UTC
Read the original article Hit count: 666

Filed under:
|
|
|

Hi,

I'm encountering a big problem when i'm trying to make a recursive search function inside a thread (using delphi 7) bellow is the code:

TParcFicDir = class(TThread)
private
 several variables..
 protected
    procedure Execute; override;
  public
    constructor Create(CreateSuspended: Boolean);


constructor TParcFicDir.Create(CreateSuspended: Boolean);
begin
 inherited Create(CreateSuspended);
end;

procedure TParcFicDir.Execute;
begin
 try
  FindFiles(FStartDir,FMask);//'c:\' and '*.*'
 except on e:Exception do
 end;
end;

procedure TParcFicDir.FindFiles(StartDir, FileMask: string);
var
  wTmp              : string;
  f:TextFile;
  wTempSR:TSearchRec;

  function Search(StartDir, FileMask: string): string;
  var
    SR              : TSearchRec;
    IsFound         : Boolean;
    files           : integer;
    dirs            : integer;
    t               : string;
  begin
    try
      files := 0;
      dirs := 0;
      if StartDir[length(StartDir)] <> '\' then
        StartDir := StartDir + '\';
      try
        IsFound := (FindFirst(StartDir + '*.*', faAnyFile, SR) = 0);// here the thread gets interrupted
      except on e: Exception do
      end;
      while IsFound do
      begin
        if (SR.Name <> '.') and (SR.Name <> '..') then
          if ((SR.Attr and faDirectory) <> 0) then
          if FScanDirs then 
          begin
            inc(dirs);
            t := Search(StartDir + SR.Name, FileMask);
            try
              files := files + strtoint(copy((t), 0, pos('#', t) - 1));//old code, don't take on calcul;
              Delete(t, 1, pos('#', t));
              dirs := dirs + strtoint(t);
            except on e: Exception do
            end;
            begin
              t := StartDir + SR.Name;
              wTmp := t;
              wtmp := '';
              Inc(FDirNo);
              writeln(f,t);
              inc(filno);
            end;
          end
          else
          if ScanFiles then 
          begin
           inc(filno);
           inc(files);
          end;
        IsFound := FindNext(SR) = 0;
      end;
      Result := IntToStr(files) + '#' + IntToStr(dirs);
      sysutils.FindClose(SR);
    except on e: Exception do
    end;
  end;
begin
 filno := 0;
  try
    try
      if trim(FPathFileTmp)<>'' then
       AssignFile(f, FPathFileTmp+'Temp.bak')
      else
       AssignFile(f,ExtractFileDir(GetDllName)+'\Temp.bak');
      Rewrite(f);
      Search(StartDir, FileMask);
      if StartDir[length(StartDir)] = '\' then
        delete(StartDir, length(StartDir), 1);
      wTmp := StartDir;
      wTmp := '';
      if FindFirst(StartDir, faDirectory, wTempSR) = 0 then
      writeln(f);
      writeln(f);
      CloseFile(f);
    except on e: Exception do
    end;
  finally
  end;
end;

ok, probably the code is a little messed up, but i don't understand why the thread ends at 'findfirst' part....i googled it, no results.

any help will be appreciated!

Thanks in advance

© Stack Overflow or respective owner

Related posts about delphi

Related posts about search