Technè

Tecnologia & Experiência do Usuário no C.E.S.A.R

bada timer

Posted by Eduardo On abril - 5 - 2011

Today we will see how to use Osp::Base::Runtime::Timer

This class can activate the timer and notify the listeners.
This timer class is not a periodic timer, only a one-shot timer. If you want to carry out periodic tasks, you must start it again after it has fired.

It’s easy to use Timers in your app… lets see:

You must extend and construct your Timer

Extend (.h):

public Osp::Base::Runtime::ITimerEventListener

Construct(.cpp):

    __pTimer = new Timer();
    r = __pTimer->Construct(*this);
    r = __pTimer->Start(1000);

Implement OnTimerExpired (to start again your Timer), as shown below:

void
MyApp::OnTimerExpired(Timer& timer)
{

    ...

    timer.Start(1000);
}

Every time your timer ends, OnTimerExpired is called (so you can start your timer again).

Full example:

class MyApp
    : public ITimerEventListener
{
   ...
public:
    result InitTimer();
public:
    void OnTimerExpired(Timer& timer);
};

void
MyApp::OnTimerExpired(Timer& timer)
{

    ...

    timer.Start(1000);
}

result
MyApp::InitTimer()
{
    result r = E_SUCCESS;

    __pTimer = new Timer;

    r = __pTimer->Construct(*this);
    if (IsFailed(r))
    {
        goto CATCH;
    }

    __pTimer->Start(1000);
    if (IsFailed(r))
    {
        goto CATCH;
    }

    return r;
CATCH:
    return r;
}

A real example – implementing a bada Chronometer:
Construct:

    keepRunning = true;
    //get actual time and set as initial time when form is loaded.
    SystemTime::GetTicks(initialTime);
    __pTimer = new Timer();
    r = __pTimer->Construct(*this);
    r = __pTimer->Start(1000);

void
MyApp::Cronometer(){
 completeTime = "00:00:00";
 int diff, hour, minutes, seconds;
    String hourTxt, minutesTxt, secondsTxt;
 if(keepRunning){
  SystemTime::GetTicks(currentTime);
                //get actual time to make diff with initial time
  diff = (currentTime - initialTime);

  hour = Math::Floor((diff/1000)/60/60);
  minutes = Math::Floor((diff/1000)/60);
  seconds = Math::Floor((diff/1000)%60);
  if (seconds <= 9){
   secondsTxt = "0" + LongLong::ToString(seconds%60);
  }else if(seconds == 60){
   secondsTxt = "00";
   minutes++;
  }else{
   secondsTxt = LongLong::ToString(seconds%60);
  }

  if (minutes <= 9){
   minutesTxt = "0" + LongLong::ToString(minutes%60);
  }else if(minutes == 60){
   minutesTxt = "00";
   hour++;
  }else{
   minutesTxt = LongLong::ToString(minutes%60);
  }

  if (hour <= 9){
   hourTxt = "0" + LongLong::ToString(hour);
  }else if(hour == 24){
   hourTxt = "00";
  }else{
   hourTxt = LongLong::ToString(hour%24);
  }

  completeTime = hourTxt + ":"+ minutesTxt + ":"+ secondsTxt;

  Label *lblChrono = static_cast

Thanks for your visit! :)

Feel free to ask/suggest/comment.
Twitter: @oliveiraeduardo

Categorias: Geral

Leave a Reply