PrevNextUpHome BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1

19.3. Timer Class

SFXTimer is the class that handles the timer.

Example 19.8. How to use the SFXTimer class

// define SFXTimer instance as class member variable
class MyClass {
private:
    SFXTimer _timer;
public:
    Void Start(Void);
    
    // callback function
    CALLBACK_DECLARE_SFXTIMER(OnTimer)
};

Void MyClass::Start(Void)
{
    SFCError error;

    // set OnTimer function called by timer
    _timer.Set(CALLBACK_FUNCTION(OnTimer));

    // set timer
    // (after 1000 milliseconds, OnTimer function will be called)
    error = _timer.Schedule(1000);

    if (error != SFERR_NO_ERROR) {
        // if error occurs (OnTimer function will not be called)
    }
}

// callback function called by the timer
CALLBACK_IMPLEMENT_SFXTIMER(MyClass, OnTimer)
{
    TRACE("timer");
    
    // if code below is added, OnTimer function will be called every 1000 milliseconds
    // _timer.Schedule(1000);

}
[Caution] Retaining the SFXTimer instance

While using the timer, the SFXTimer instance must not be released.

Example 19.9. Cancel the timer

// cancel the timer
// Cancel function may be called from anywhere
_timer.Cancel();

The SFXTimer::Cancel function is also called in the destructor of SFXTimer.

In the BREW environment, one processing must be finished within about 1 second. Therefore, the processing that takes more than 1 second must be divided into segments that take less than 1 second, and then execute each segment sequentially by using the SFXTask class.

Example 19.10. Divide task

// define SFXTask instance as class member variable
class MyClass {
private:
    SFXTask _task;
    SInt32 _index;
    SInt32 _sum;
public:
    Void Start(Void);
    SInt32 Calculate(SInt32 index);

    // callback function
    CALLBACK_DECLARE_SFXTIMER(OnTask)
};

Void MyClass::Start(Void)
{
    SFCError error;

    _index = 1;
    _sum = 0;

    // set Ontask function called by BREW environment
    _task.Set(CALLBACK_FUNCTION(OnTask));

    // schedule task
    error = _task.Schedule();

    if (error != SFERR_NO_ERROR) {
        // if error occurs(OnTask function will not be called)
    }
    
    // control is returned to BREW environment after this code ended,
    // Ontask function will be called by BREW environment after a while
}

// time consuming calculation
SInt32 MyClass::Calculate(SInt32 index)
{
    SInt32 i;
    for (i = 0; i < 5000000; ++i) {
        ;
    }
    return index;
}

// callback function called by BREW environment
CALLBACK_IMPLEMENT_SFXTIMER(MyClass, OnTask)
{
    SFCError error;

    if (_index <= 100) { // divide calculation into 100 segments
        // calculate with _index
        _sum += Calculate(_index);
        ++_index;
        // schedule task agian (OnTask function will be called again by BREW environment)
        error = _task.Schedule();

        if (error != SFERR_NO_ERROR) {
            // if error occurs (OnTask function will not be called)
        }
    }
    else {
        // display result
        TRACE("%d", _sum);
    }
}

The above code is equivalent with the following.

SInt32 i;
SInt32 sum = 0;
for (i = 1; i <= 100; ++i) {
    sum += Calculate(i);
}