Solved

Thread in Delphi - time-problem

Posted on 2013-11-26
6
542 Views
Last Modified: 2013-11-26
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??
0
Comment
Question by:formi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 39677938
what is the rest of your code ?

the only thing this thread does is sleep ... every 30 seconds
0
 
LVL 38

Accepted Solution

by:
Geert Gruwez earned 250 total points
ID: 39677944
procedure TPdaSyncThread.doProcess;
begin
  // ... call your AppMessage here
end;

procedure TPdaSyncThread.Execute;
begin
  while not Terminated do
  begin
    Sleep(30000);
    Synchronize(self,doProcess);
  end;
end;
0
 
LVL 19

Assisted Solution

by:MerijnB
MerijnB earned 250 total points
ID: 39677973
Why do you call Synchronize? that will make the sleep happen in main thread, exactly what you don't want?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:formi
ID: 39677993
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.
0
 

Author Closing Comment

by:formi
ID: 39678004
Thanks you helped me both!
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 39679967
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question