Board index » cppbuilder » Timer and TDateTime problem

Timer and TDateTime problem


2006-01-18 12:36:42 AM
cppbuilder47
hi guys,
at the moment i need an application which is running 24 hours a day. i like
to start an process each day at 11:00 clock. i created an application with
an timer with and period of 60000 ms.
this here is my code:
String lastrun;
TDateTime current_time;
TDateTime current_date;
void __fastcall TForm1::TimerTimer(TObject *Sender)
{
// get current time and date
current_time = current_time.CurrentTime();
current_date = current_date.CurrentDate();
// the time when to run the process (each day at 11:00:00)
TDateTime time = TDateTime("11:00:00");
// check if it is alreasy 11:00:00 and the process didn't run today
if (current_time>= time && lastrun != current_date)
{
lastrun = current_date;
LLogger->Items->Add("run test on " + AnsiString(Date()) + " - " +
AnsiString(Time()));
//TExecute * thread = new TExecute(true);
}
}
//---------------------------------------------------------------------------
it works fine, but only for one day. the seconds, third, .... nothing
happens. there should be some problmes with the datetime, but i dont know
what it wrang.
could anybody check this code for errors?
greetings
LillyChamp
 
 

Re:Timer and TDateTime problem

"LillyChamp" < XXXX@XXXXX.COM >wrote:
Quote

<snip>
TDateTime time = TDateTime("11:00:00");
TDateTime is a date/time field and you're using it as if it
were a single type of your choice. If you're going to compare
only the time or date of a TDateTime, you need to extract it
from the variable. However, it would be easier if you
constructed the TDateTime variables to include both date and
made a single compare.
I would also suggest that you use the registry and a seperate
thread that you put to sleep for half of the remaining time
before you want it to execute.
By using the registry, if the system restarts for any reason,
you won't get an extra process spawned when you didn't want it.
~ JD
 

Re:Timer and TDateTime problem

Hi JD,
the code i showed is only a little piece to express the problem. my
application manage different process. that is why i don't like to use the
registry or further threads.
for some process i will need to compare only the time and sometimes i need
to compare the date.
i have no idea how to extract the time and date from TDateTime and also do
comparsion as well.
could you give me an example how to do this, so that i can correct my code.
greetings from germany
LillyChamp
"JD" < XXXX@XXXXX.COM >schrieb im Newsbeitrag
Quote

"LillyChamp" < XXXX@XXXXX.COM >wrote:
>
<snip>
>TDateTime time = TDateTime("11:00:00");

TDateTime is a date/time field and you're using it as if it
were a single type of your choice. If you're going to compare
only the time or date of a TDateTime, you need to extract it
from the variable. However, it would be easier if you
constructed the TDateTime variables to include both date and
made a single compare.

I would also suggest that you use the registry and a seperate
thread that you put to sleep for half of the remaining time
before you want it to execute.

By using the registry, if the system restarts for any reason,
you won't get an extra process spawned when you didn't want it.

~ JD

 

{smallsort}

Re:Timer and TDateTime problem

LillyChamp wrote:
Quote
could you give me an example how to do this, so that i can correct my code.
Why are you using global variables ? Or are they classmembers ?
You compare AnsiStrings with TDateTime objects. That makes your code
unreadable. You log time and date but not the actual values
you use for comparison.
Try this:
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
// get current time and date
TDateTime DateTime = DateTime.CurrentDateTime();
// the time when to run the process (each day at 11:00:00)
TDateTime time = TDateTime("11:00:00");
static AnsiString LastDateString;
// check if it is alreasy 11:00:00 and the process didn't run today
if (DateTime.TimeString()>= time.TimeString() && DateTime.DateString() != LastDateString )
{
LastDateString = DateTime.DateString();
//TExecute * thread = new TExecute(true);
}
}
Hans.