Define and build lapsed timer

829 Views Asked by At

In Delphi, I understand how to build lapsing timer. But I am not sure about how to write code for C++Builder. I could not find any example.

In Delphi I wrote this code below, a copy from the source somewhere:-

....
type
  TFrame2 = class(TFrame)
StatusBar1: TStatusBar;
Timer1: TTimer;

constructor TFrame2.Create(TheOwner: TComponent);
 begin
   inherited Create(TheOwner);
   StartTime := Now;
   Timer1.Enabled := True;
 end;

destructor TFrame2.Destroy;
 begin
   inherited Destroy
 end;

procedure TFrame2.Timer1Timer(Sender: TObject);//This event occurs every second.
Var
Hour, Min, Sec, MSec : Word;
Diff : TTime;
 begin
Timer1.Enabled := False;
Diff := Now - StartTime;
DecodeTime(Diff, Hour, Min, Sec, MSec);
StatusBar1.Panels.Items[1].Text := IntToStr(Min)+' Minutes, '+IntToStr(Sec)+' Seconds.';
Timer1.Enabled := True;
end;

...

Please kindly how to do same in C++. Thanks

1

There are 1 best solutions below

1
On BEST ANSWER

Try this:

....
class TFrame2 : public TFrame
{
__published:
    TStatusBar *StatusBar1;
    TTimer *Timer1;
    ...
    void __fastcall Timer1Timer(TObject *Sender);
    ...
private:
    TDateTime StartTime;
    ...
public:
    __fastcall TFrame2(TComponent *TheOwner);
};

__fastcall TFrame2::TFrame2(TComponent *TheOwner)
    : TFrame(TheOwner)
{
    StartTime = Now();
    Timer1->Enabled = true;
}

void __fastcall TFrame2::Timer1Timer(TObject *Sender) //This event occurs every second.
{
    Timer1->Enabled = false;

    TDateTime Diff = Now() - StartTime;
    Word Hour, Min, Sec, MSec;
    DecodeTime(Diff, Hour, Min, Sec, MSec);
    StatusBar1->Panels->Items[1]->Text = String(Min)+" Minutes, "+String(Sec)+" Seconds.";

    Timer1->Enabled = true;
}

...

Alternatively, you can simplify Timer1Timer() to this:

void __fastcall TFrame2::Timer1Timer(TObject *Sender) //This event occurs every second.
{
    // this is not overhead-intense code, so
    // stopping and re-starting the timer
    // is wasting unnecessary processing time...

    //Timer1->Enabled = true;

    TDateTime Diff = Now() - StartTime;
    StatusBar1->Panels->Items[1]->Text = Diff.FormatString("n' Minutes, 's' Seconds.'");

    //Timer1->Enabled = true;
}

Personally, I would not use the system clock at all, in case the user changes the clock, or it auto-rolls for DST, while your timer is running. I would use CPU ticks instead, either manually:

....
class TFrame2 : public TFrame
{
__published:
    TStatusBar *StatusBar1;
    TTimer *Timer1;
    ...
    void __fastcall Timer1Timer(TObject *Sender);
    ...
private:
    DWORD StartTime;
    ...
public:
    __fastcall TFrame2(TComponent *TheOwner);
};

__fastcall TFrame2::TFrame2(TComponent *TheOwner)
    : TFrame(TheOwner)
{
    StartTime = GetTickCount();
    Timer1->Enabled = true;
}

void __fastcall TFrame2::Timer1Timer(TObject *Sender) //This event occurs every second.
{
    //Timer1->Enabled = false;

    DWORD Diff = GetTickCount() - StartTime;
    DWORD Mins = Diff / 60000; Diff %= 60000;
    DWORD Secs = Diff / 1000;

    StatusBar1->Panels->Items[1]->Text = String(Mins)+" Minutes, "+String(Secs)+" Seconds.";

    //Timer1->Enabled = true;
}

...

Or via TStopWatch:

#include <System.Diagnostics.hpp>

....
class TFrame2 : public TFrame
{
__published:
    TStatusBar *StatusBar1;
    TTimer *Timer1;
    ...
    void __fastcall Timer1Timer(TObject *Sender);
    ...
private:
    TStopwatch SW;
    ...
public:
    __fastcall TFrame2(TComponent *TheOwner);
};

__fastcall TFrame2::TFrame2(TComponent *TheOwner)
    : TFrame(TheOwner)
{
    SW = TStopwatch::StartNew();
    Timer1->Enabled = true;
}

void __fastcall TFrame2::Timer1Timer(TObject *Sender) //This event occurs every second.
{
    //Timer1->Enabled = false;
    SW.Stop();

    TTimeSpan TS = SW.Elapsed;
    StatusBar1->Panels->Items[1]->Text = String(TS.Minutes)+" Minutes, "+String(TS.Seconds)+" Seconds.";

    SW.Start();
    //Timer1->Enabled = true;
}