Link to home
Start Free TrialLog in
Avatar of formi
formiFlag for Switzerland

asked on

Thread in Delphi - time-problem

Hi

In an application I comunicate with a mobile device. If the device is not in the dockingstation it is possible, that calling a function will respond after a timeout of i.e. 60 seconds. So my idea was to program this in a thread.
  TPdaSyncThread = class(TThread)
  private
    procedure doProcess;
  protected
    procedure Execute; override;
  public
    constructor Create(... myParams);
  end;

constructor TPdaSyncThread.Create(... myParams);
begin
  FreeOnTerminate := True;
  inherited Create(False);
  ....
end;

procedure TPdaSyncThread.doProcess;
begin
  Sleep(30000);  //just to "simulate" calling the function that will return after a timeout
end;

procedure TPdaSyncThread.Execute;
begin
  while not Terminated do
  begin
    Synchronize(self,doProcess);
  end;
end;

Open in new window

In the main-thread I have a procedure "AppMessage" (Application.Onmessage := "AppMessage". During the 30 seconds (sleep) this procedure is never called. I think I didn't understand something with threading??
Avatar of Geert G
Geert G
Flag of Belgium image

what is the rest of your code ?

the only thing this thread does is sleep ... every 30 seconds
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of formi

ASKER

Yes you are right. it only sleeps - as I wrote in the code's comment to simulate the long answer-time of a routine. But I think you gave me the tip: is it correct that during calling a routine with "Synchronize" no other thread gets time to execute? If "Yes" I have to call the time-consuming routines in the execute-loop and not in the synchronize-routine.
Avatar of formi

ASKER

Thanks you helped me both!
make sure all procedures in synchronize are short
and don't update/draw anything on other forms from inside a thread

you'll have circular unit reference to start with
the code may be simple but it will difficult to follow the flow of just 1 unit