Solved

Thread in Delphi - time-problem

Posted on 2013-11-26
6
528 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 37

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 37

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 37

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

777 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