Recent

Author Topic: FindAllFiles  (Read 9646 times)

tjpren

  • Jr. Member
  • **
  • Posts: 67
FindAllFiles
« on: December 02, 2011, 11:28:10 am »
Hi Guys,

I've written a basic search program.
I'm using the FindAllFiles Procedure, and it is working very well.
I'd like some indication that a search is active, and so I've created a button that changes colour during a search. However it is not working.

If I don't execute the FindAllFiles, the button changes.  If I do execute a search, the button doesn't change.  Something about the search stops the button colour change.

Does anyone know of an event in the FindAllFiles that I could yse instead?

Thanks

 

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: FindAllFiles
« Reply #1 on: December 02, 2011, 11:49:50 am »
This works fine:

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  Path, Mask :string;
begin
  Path := 'c:\';
  Mask := '*.txt';
  Shape1.Brush.Color := clRed;
  Memo1.Lines.Clear;
  Application.ProcessMessages;
  Memo1.Lines := FindAllFiles(Path, Mask, True);
  Shape1.Brush.Color := clLime;
  Application.ProcessMessages;
end; 
« Last Edit: December 02, 2011, 12:03:06 pm by typo »

tjpren

  • Jr. Member
  • **
  • Posts: 67
Re: FindAllFiles
« Reply #2 on: December 02, 2011, 01:12:12 pm »
Hello, thanks for this.

It doesn't seem too different to mine - I'm just calling a procedure and dumping the results into a list box.  I've found that if I add a messagebox after I make the colour change but before I make the search, the shape does change colour.  It's as if the colour change takes time, and gets interrupted by search.

Code: [Select]
Procedure TForm1.Search;
var
  sl: TStringList;
  i: Integer;
begin
  sl := FindAllFiles(Edit1.Text, Edit2.Text, True);
  try
    //Memo1.Append('There are ' + IntToStr(sl.Count) + ' pascal files in directory');
    for i:=0 to sl.Count-1 do
        //Memo1.Append('File'+IntToStr(i)+': ' + sl.Strings[i]);
          ListBox1.Items.Add('File'+IntToStr(i)+': ' + sl.Strings[i]);
    finally
    sl.Free;
    Shape1.Brush.Color :=clGreen;
  end;
end; 
procedure TForm1.Button1Click(Sender: TObject);
begin
  Shape1.Brush.Color :=clRed;
  Search;
end;


eny

  • Hero Member
  • *****
  • Posts: 1647
Re: FindAllFiles
« Reply #3 on: December 02, 2011, 01:17:09 pm »
It doesn't seem too different to mine
Any idea why typo put ' Application.ProcessMessages;' in there, which is a huge difference compared to your code?
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: FindAllFiles
« Reply #4 on: December 02, 2011, 01:35:54 pm »
Yes, this is the problem.

BTW, you use FindAllFiles correctly, very good. My code has probably a memory leak.
« Last Edit: December 02, 2011, 01:54:52 pm by typo »

tjpren

  • Jr. Member
  • **
  • Posts: 67
Re: FindAllFiles
« Reply #5 on: December 03, 2011, 12:09:41 am »
Thanks for the help, and pointing out the Application.ProcessMessages.

I've added the instructions to my code and the color change on the button now works OK.

If I understand it correctly, it allows time consuming threads to be completed concurrently, giving low priority tasks such as the button change the opportunity to complete.

I'll have to read more in the wiki, on how and when to use it.

Regards :D

 

TinyPortal © 2005-2018