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
In your posted code I see that you try catch and forget the exception. When you have broken code that contains something like that, the very first thing I do is remove the empty except block. Odds are that don’t know what is going on because your throwing the information you need away.
You have two calls to
FindFirst()in your code. The usage of the one where your trying to find a directory is suspect. I would change your code to something like this.Then you will see the reason
FindFirst()Failed.Instead of using
FindFirst()in the case where your looking for the directory I would use theDirectoryExists()call instead.