Solved

Thread in Delphi - time-problem

Posted on 2013-11-26
6
517 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
  • 3
  • 2
6 Comments
 
LVL 36

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 36

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 36

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now