Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Thread in Delphi - time-problem

Posted on 2013-11-26
6
Medium Priority
?
547 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 1000 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

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!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

730 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