Link to home
Start Free TrialLog in
Avatar of Member_2_7965240
Member_2_7965240

asked on

Delphi: timer at exact time

Hello,

I need a timer which does something exactly at specified times , 10 minutes starting from each hour
e.g. 10:00, 10:10, 10:20 ... 10:50, 11:00,11:10 etc.

So if the user starts my program between 9:51 and 10:00  the first timer tick should be at 10:00, if he starts my program between 10:01 and 10:10  the first timer tick should be at 10:10 etc. -> and then set the next tick to the next 10 minutes.

How can I do this? Thank you.
Avatar of LesterClayton
LesterClayton
Flag of Norway image

There is no way to do this that I know of, but what i'd do is make the timer interval 100 (every 1/10th of a second), and use the DateTime functions to read the time.  When it hits 0 minutes and 0 seconds, then you change the interval to be 60 minutes (3600000 seconds), and also set a global boolean variable to true.

Your timer should also check the global boolean variable, and run your desired code if it's true.
Avatar of ste5an
What is your concrete use-case?

Doing this reliable is pretty complicated. But I would use a separate thread. Calculate the time difference till the next execution and do a WaitForMultipleObjects. Execute your workload. Repeat.
Avatar of Member_2_7965240
Member_2_7965240

ASKER

The mobile application should start a remote database selection (via Internet) which is both time and resource consuming.
After this it should send messages depending on the result.

The users can set the intervals (which are 10:00, 10:10, 10:20 ... 10:50, 11:00,11:10 etc.).
hmm, I'm getting a headache.. Did you said "mobile"? Can you elaborate this further? Describe your context. It sounds more like some architectural question of the back-end.
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you are asking for a slow process to start at an exact time ?

let's assume the slow process take 10 seconds and you succeed in starting it exactly at 10:00:00
your answer would arrive at 10:00:10

why not make the slow process 5 seconds faster ?

that would give you a margin to start within 5 seconds
and the user would get answer within 10 second window

are you assuming the user actually has a good internet connection ?
what if he doesn't ?

wouldn't it be pointless to start the process in the case the user doesn't have a connection ?
or has actually no connection at all ?

if you do succeed in starting exactly on time,
there is no guarantees on the speed of the mobile network, so what's the point in starting exactly on a certain time
when your answer will return in different intervals based on the mobile network speed ?
oh yeah,
you might want to postpone the next attempt until the current attempt has either succeeded, failed or timed out
Ok, then a little bit more explanation:

1. The user will create messages in his program (message text and sending time)
The sending time cannot be whatever the user wants (he cannot type in) he will choose the date from date edit, hour from a combo box, and the minutes from a combobox (which is 00,10,20,30,40 or 50)

2.The other program starts (it is a multi device application, but it doesn't matter in this context)
It will start the timer as I explained above.
Let's say it is 10:00 -> it will query the database table which messages are to be sent at this time? If there are messages it will send them.
At next it will check at 10:10 > it will query the database table which messages are to be sent at 10:10? etc.
(it is a multi device application, but it doesn't matter in this context)

It for sure does. Cause there are different IPC solutions possible depending on the kind of devices and infrastructure.

well, but with the given context: Just use the Windows Task Scheduler (or cron) to start your sending program.
Then in an addition to my code - you need to check database for messages below calling time and not send it yet....
This all must be in a thread too...
create a record in the database with the sending times
let the database do the rest

creating a record either works or fails, but it's not so time dependant

a database like oracle can do that
that reminds me ... it's sending me a lot of spam mail/sms of tablespaces to extend, jobs failing, backup jobs
still lots of things to do
Sinisa Vuk: function TimeToFire should return TDatetime and not Integer, right?
Because Result  := IncMinute(dt, iMinutesBetweenTwo * ((mm div iMinutesBetweenTwo) + 1)    ); returns  TDatetime

Thanks
fwiw ...
you ask an exact time and then accept a question with minute precision ?

rather contradictory ... no ?
Hello Geert Gruwez,

I my question it was described in detail:

I need a timer which does something exactly at specified times , 10 minutes starting from each hour
e.g. 10:00, 10:10, 10:20 ... 10:50, 11:00,11:10 etc.
....

So in short : exact time every 10 minutes. So exactly at 10:10, exactly at 10:20, exactly at 10:30 etc.
Or I can say that exactly at 10:10:00:00, exactly at 10:20:00:00, exactly at 10:30:00:00 etc.
if your timer fires exactly at midnight on 31 december ...
not much change anything gets processed before the next time your timer fires