Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'working, please wait' screen with thread?

Perhaps, it is very easy for you, but I am hard working on a project (for educational purposes) that is querying adsi with TADSISearch component, for several days. I'm trying to show a 'Working, Please wait..' splash screen with a man worker animated gif on Form2 while TADSISearch is searching the Active Directory. Although i tried every possibilities according to me, but i couldn't succeed. I tried to use TADSISearch in a thread, but thread is terminating before ADSIsearch finishes. I think TADSISearch is not thread safe. What do you think? Also, another way that I created Form2 and used a thread for updating it but the animated gif is stopping while main form gone adsi searching. What can you say about these? How can i make a please wait screen while ADSISearch is working and keep main form responding. Application.ProcessMessages or timer is not a way too. Thanks a lot for reading and answers.

like image 793
Knn Avatar asked Dec 17 '22 03:12

Knn


2 Answers

The graphical user interface should be updated by the main thread. You should put your search code into a separate thread, and while the searcher thread is working, your main thread can show the animation along with "Please wait" message.

Your searcher thread can notify the main thread when search is done by any of the available synchronization techniques. The simplest one is to define a method in your thread class which stops the animation in user interface, and pass that method to Synchronize at the end of Execute method of your searcher thread.

Your searcher thread code will be something like this:

type
  TMyThread = class(TThread)
  private
    procedure NotifyEndOfThread;
  protected
    procedure Execute; override;
  end;

implementation

uses MainFormUnit;

procedure TMyThread.NotifyEndOfThread;
begin
  MainForm.ShowAnimation := False;
end;

procedure TMyThread.Execute;
begin
  try
    {Add your search code here}
  finally
    Synchronize(NotifyEndOfThread);
  end;
end;

And your main thread's code will be like this:

TMainForm = class(TForm)
...
private 
  FShowAnimation : Boolean;
  procedure SetShowAnimation(Value: Boolean);
public
  property ShowAnimation : Boolean read FShowAnimation write SetShowAnimation;
end;

procedure TMainForm.SetShowAnimation(Value: Boolean);
begin
  FShowAnimation := Value;
  if FShowAnimation then
    {Add animation code here}
  else
    {Stop animation}
end;
like image 87
vcldeveloper Avatar answered Jan 04 '23 09:01

vcldeveloper


Maybe you can try this:

Threaded Splashscreen for Delphi
http://cc.embarcadero.com/Item/20139

I use this on a touchscreen/terminal application (thin client, Wifi, RemObjects, etc) and it works nice! Also got an animated gif working.

like image 32
André Avatar answered Jan 04 '23 08:01

André