Delphi 'Alarm Clock' app

I need to make a simple alarm application that instead of playing sound will output the file to ftp (received the last value). Timers have proven ineffective when it comes to thread execution.

Here is what I got so far:

var
ttime     : tDateTime;
timerstr  : string;
timealarm : string;
aThread : TMyThread;
begin
aThread := tMyThread.Create(false);
ttime := Now;
timestr := FormatDateTime('hh:nn:ss', ttime);
timealarm := '09:30:30';
if timestr = timealarm then
aThread.Resume; //The thread will execute once and terminate;
end;

Can you guys think of another way to make this command once a day in a more efficient way?

Thank.

+5
source share
3 answers

solution found : CRON Scheduler

Thanks to LU RD and Runner.

+6
source

. , TDateTime , >=, =, , , , .

procedure TForm1.Timer1Timer(Sender: TObject);
var
  currentTime: TDateTime;
  alarmTime: TDateTime;
begin
  // Time is a floating point value with everything to the left of the zero
  // representing the days, and everything to the right of the decimal
  // point representing time of day

  // Date() gets just the day portion, and lastDateExecuted is a global TDateTime
  // variable where we store the last day the program ran
  // We only go further if it didn't execute today
  if (Date > lastDateExecuted) then
  begin
    // Use Time() instead of Now() to get just the time portion and leave the day
    // portion at 0
    currentTime := Time;
    // Covert our alarm string to TDateTime instead of TDateTime to string
    alarmTime := EncodeTime(9, 30, 30, 0);
    // Is it time to run?
    // Greater than or equal comparison makes the application still run in case
    // our timer happens to miss the exact millisecond of the target time
    if currentTime >= alarmTime then
    begin
      // Immediately set the last run date to keep the next timer event
      // from repeating this
      lastDateExecuted := Date;
      // Do your work here
      DoMyThing;
    end;
  end;
end;

lastDateExecuted 0.

. , , . , , .

+3

, , , - , . , , ( 500 , 1 60 000 120 , ). , .

( ), /. : , . , . , ...

uses
  DateUtils;

.....

var
  HasExecuted: Bool;

.....

constructor TForm1.Create(Sender: TObject);
begin
  HasExecuted:= False;
end;

procedure TimerOnTimer(Sender: TObject);
var
  N, A: TDateTime;
  Thread: TMyThread;
begin
  N := Now;
  A := StrToDateTime('11/20/2011 15:30:30'); //24 hour time
  //If now is within 1 second over/under alarm time...
  if (N >= IncSecond(A, -1)) and (N <= IncSecond(A, 1)) then
  begin
    if not HasExecuted then begin
      HasExecuted:= True;
      aThread := tMyThread.Create(true);
      aThread.Resume; //The thread will execute once and terminate;  
    end;
  end;
end;

, , =. , 1 ? , , true.

, TMyThread - ? , False CreateSuspended? , , . True , , , Thread.Resume (, , ). , ( , ), . , ? ( , , , ). , , , , ...

constructor TMyThread.Create(CreateSuspended: Bool);
begin
  inherited Create(CreateSuspended);
  FreeOnTerminate:= True; //Make sure it free its self when it terminated
end;

EDIT:

- - , , 1 ( 200-400), , . , , . . , delphi.

+1
source

All Articles