Si lo ejecuto de esa manera mi aplicación no responderá hasta encontrar todos los archivos en el listbox mi pregunta es ¿Cómo puedo hacer que esta función se multiplique para evitar situaciones que no correspondan? todavía estoy Delphi novoicebúsqueda de archivos delphi multiprocesamiento
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;
+1, solución simple y limpia. No hay mucho que pueda salir mal. –
No debería el 'PostMessage (MainForm.Handle, WM_FILESEARCH_FINISHED, 0, 0); 'ser utilizado con' Sincronizar'? – kobik
@kobik, no hay necesidad de preocuparse, la propiedad 'Handle' es de solo lectura y su lectura es atómica. Y tiene que ser diferente de 0 (el control estaría muerto y no podría recibir ningún mensaje), por lo que el getter nunca 'CreateHandle' hará lo mismo cuando el handle sea 0. El riesgo es la instancia del objeto en sí, si destruye 'MainForm' e intenta acceder a él, obtendrás AV como siempre. Y, Emba ['uses'] (http://edn.embarcadero.com/article/22411) esto también :-) – TLama