Jump to content
Alexandre Pires

How to read a table every x time.

Recommended Posts

Hi Guys, I need my Android background service to read a table from my database every 30 seconds. What would be the best way to do this? 

I am currently using an infinite loop in a thread and sleep, but I believe it is not the best approach.

@Dave Nottage Is there any kastri component that helps to do this?

 

I am using Delphi 10.3

Thanks for some help

Alexandre

Share this post


Link to post
13 hours ago, Alexandre Pires said:

@Remy Lebeau do you mean ttimer component? But does ttimer work on services?

No, not in background.

But in background you need maybe also other measures to keep Android awake.

Share this post


Link to post

Hello,

   do not use sleep in background services, Android can think that Your service is not working (especially in energy save mode) and try to kill. Instead of this use for example :

 

const cTimeInterval = 30000;
var aLastTime : TDateTime;
begin
  aLastTime := Now();  // use aLastTime := Now()-1; (whatever to start imiditly)
  while true do
    begin
      if (MilliSecondsBetween(Now(),aLastTime)>=cTimeInterval) then
      begin       
        aLastTime := Now();        
        // do somethink there
      end;
    end;
end;

if You need to have time interval counting after to do something move aLastTime := Now(); after work you need to make. For me works better if I create that in new Thread in service.

while true do - you can change to do somethink to brake loop if you need

 

best regards
Moskw@

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×