How to handle the Timer stuff?

Hi Folks!

Here's what I want:

A buttonclick should start a timer and a label should display the count down in seconds.
Now suppose I'm archiving all my porn pics for example:) I have to do that within say 5 minutes and if I succeed I want to know how long it took me to archive all that stuff.
This information should be saved to a textfile along with some other info but I can take care of that I just need to handle the timer thingie related stuff.

That's it actually,

Regards and maximum respect to all-0-ya out there!
Please provide copy&paste code and if you need more info just lemme know!

Peter
PeterdeBAsked:
Who is Participating?
 
TheRealLokiSenior DeveloperCommented:
ok, i think this does everything. let me know if I missed something

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

const
    COUNTDOWN_TIME = (1/24/30); //2 minutes
type
  TForm1 = class(TForm)
    bStartTimer: TButton;
    Label1: TLabel;
    Timer1: TTimer;
    bStopTimer: TButton;
    procedure bStartTimerClick(Sender: TObject);
    procedure bStopTimerClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
    StartTime, ExpectedEndTime, EndTime: TDateTime;
    procedure AddToLog(S: string);
    function DurationAsString(Duration_: TDateTime): string;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

// either creates or adds to the end of a log file in the same directory as the exe, called log.txt
procedure TForm1.AddToLog(S: string);
    const
        logfilename = 'log.txt';
    var
        TextF: TextFile;
    begin
        AssignFile(TextF, ExtractFilePath(Paramstr(0)) + logfilename);
        if FileExists( ExtractFilePath(Paramstr(0)) + logfilename) then
          Append(TextF)
        else
          Rewrite(TextF);
        WriteLn(TextF, S);
        CloseFile(TextF);
    end;

procedure TForm1.bStartTimerClick(Sender: TObject);
    begin
        StartTime := Now; // set the start time
        ExpectedEndTime := Now + COUNTDOWN_TIME;
// another way to do it is ExpectedEndTime := Now + EncodeTime(0, 1, 0, 0); = 1 minute;
        Timer1.Enabled := True;
    end;

// This function is just to make the duration look nice
function TForm1.DurationAsString(Duration_: TDateTime): string;
    var
        h, n, s, ss: word;
        days_string: string;
        hours_string, minutes_string, seconds_string: string;
    begin
        if Trunc(Duration_) = 0 then
          days_string := ''
        else if Trunc(Duration_) = 1 then
          days_string := IntToStr(Trunc(Duration_)) + ' day'
        else
          days_string := IntToStr(Trunc(Duration_)) + ' days';

        DecodeTime(Duration_, h, n, s, ss);
        if h = 1 then
          hours_string := '1 hour'
        else if h <> 0 then
          hours_string := IntToStr(h) + ' hours';

        if n = 1 then
          minutes_string := '1 minute'
        else if n <> 0 then
          minutes_string := IntToStr(n) + ' minutes';

        if s = 1 then
          seconds_string := '1 second'
        else if s <> 0 then
          seconds_string := IntToStr(s) + ' seconds';

        result := '';
        if days_string <> '' then
          result := days_string +', ';
        if hours_string <> '' then
          result := result + hours_string + ', ';
        if minutes_string <> '' then
          result := result + minutes_string + ', ';
        if seconds_string <> '' then
          result := result + seconds_string;

        while ( (length(result) > 0) and
          (
        (result[length(result)] = ',') or
          (result[length(result)] = ' ')
        )
        ) do
          delete(result, length(result), 1);
    end;

procedure TForm1.bStopTimerClick(Sender: TObject);
    begin
        Timer1.Enabled := False;
        EndTime := Now;
        Label1.Caption := 'Process took ' + DurationAsString(EndTime - StartTime);
        AddToLog('Process took ' + DurationAsString(EndTime - StartTime));
// if you do not want it to look nice you could simply do
//        AddToLog('Process took ' + FormatDateTime('hh:nn:ss' , EndTime - StartTime));
    end;

procedure TForm1.Timer1Timer(Sender: TObject);
    begin
        if (Now > ExpectedEndTime) then
        begin
            Timer1.Enabled := False;
            AddToLog('You failed to complete your task in time');
            Label1.Caption := 'You failed to complete your task in time';
        end
        else
          Label1.Caption := DurationAsString(ExpectedEndTime - Now);
    end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
        Timer1.Enabled := False;
    end;

end.


***************** FORM FOLLOWS *******************

object Form1: TForm1
  Left = 274
  Top = 213
  Width = 362
  Height = 181
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnClose = FormClose
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 176
    Top = 40
    Width = 32
    Height = 13
    Caption = 'Label1'
  end
  object bStartTimer: TButton
    Left = 56
    Top = 40
    Width = 75
    Height = 25
    Caption = 'Start Timer'
    TabOrder = 0
    OnClick = bStartTimerClick
  end
  object bStopTimer: TButton
    Left = 56
    Top = 80
    Width = 75
    Height = 25
    Caption = 'Stop Timer'
    TabOrder = 1
    OnClick = bStopTimerClick
  end
  object Timer1: TTimer
    Enabled = False
    OnTimer = Timer1Timer
    Left = 72
    Top = 8
  end
end
0
 
shaneholmesCommented:
Drop two TButtons on your form:

btnStart & btnStop


drop a TTimer & a TLabel as well on your form

Set The TLabels caption = '00:00:00';


set the two button's OnClick events to the code below

and set the ttimer's onTimer event to the code below

and set the forms on activate


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    btnStart: TButton;
    Label1: TLabel;
    Timer1: TTimer;
    btnStop: TButton;
    procedure Timer1Timer(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure btnStartClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  SS, MM, HH: Integer;

implementation

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
 SS:= SS + 1;
 if SS = 60 then
 begin
  SS:= 0;
  MM:= MM + 1;
  if MM = 60 then
  HH:= HH + 1;
 end;
 Label1.Caption:= Format('%.2d',[HH]) + ':' + Format('%.2d',[MM]) + ':' + Format('%.2d',[SS]);
 Application.ProcessMessages;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
 SS:= 0;
 MM:= 0;
 HH:= 0;
end;

procedure TForm1.btnStartClick(Sender: TObject);
begin
 Timer1.Enabled:= True;
end;

procedure TForm1.btnStopClick(Sender: TObject);
begin
  Timer1.Enabled:= False;
end;

end.




NOte, you could also add a Pause button


procedure TForm1.btnPauseClick(Sender: TObject);
begin
 Timer1.Enabled:= False;
end;

procedure TForm1.btnStartClick(Sender: TObject);
begin
 Timer1.Enabled:= True;
end;

procedure TForm1.btnStopClick(Sender: TObject);
begin
  Timer1.Enabled:= False;
  Label1.Caption:= '00:00:00';
  SS:= 0;
  MM:= 0;
  HH:= 0;
end;

Hope this helps!

Sholmes

0
 
PeterdeBAuthor Commented:
Hi Guys!

First of all tnx for your rapid responses!
I chose for the code of TheRealLoki, judged upon the save-file-thingie. You surely didn't miss out on anything. I simply translated your messages into Dutch et voila I exactly got what I was after, simply perfect!
Shane, tnx for your reply.

Regards and respect to all-o-ya out here on EE! Simply brilliant!

Peter :)
0
 
shaneholmesCommented:
You clearly stated - "to a textfile along with some other info but I can take care of that"


sholmes
0
 
PeterdeBAuthor Commented:
Oeps, I overlooked that and it was only after I noticed the presence of that code that I decided to use TheRealLokies code.
Reply to my following question. I will name it Shane and a simple Yes will do, okay?
My apoligize for this misunderstanding. I had forgotten about it already.

Regards Peter
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.