Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to handle the Timer stuff?

Posted on 2005-04-05
5
Medium Priority
?
232 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:PeterdeB
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 13713346
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
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 500 total points
ID: 13713733
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
 

Author Comment

by:PeterdeB
ID: 13716308
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
 
LVL 11

Expert Comment

by:shaneholmes
ID: 13716326
You clearly stated - "to a textfile along with some other info but I can take care of that"


sholmes
0
 

Author Comment

by:PeterdeB
ID: 13717453
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question