Skip to content

ATimer#

Async timer.

Header:#include <AUI/Common/ATimer.h>
CMake:aui_link(my_target PUBLIC aui::core)

Detailed Description#

Emits fired periodically with the specified period until stopped. The timer does not start on its own; call start(). The first emission happens after one period elapses.

class Clock: public AObject {
public:
    Clock() {
        connect(mTimer->fired, me::tick);
        mTimer->start();
    }

    void tick() {
        ALogger::info("Clock") << "tick";
    }

private:
    _<ATimer> mTimer = _new<ATimer>(1s);
};

fired is emitted from a dedicated timer thread. When the receiving object has AObject::setSlotsCallsOnlyOnMyThread enabled (views do by default), the slot runs on the receiver's thread instead.

stop() and destroying the timer cancel any pending emission. restart() stops the timer and starts it again, so the next emission occurs after a full period.

See Background tasks and timers for more on background work.

Examples#

examples/7guis/timer/src/main.cpp

7GUIs Timer — Timer example.

        mTimer->start();
    }

private:
    _<ATimer> mTimer = _new<ATimer>(100ms);
    high_resolution_clock::time_point mStartTime = high_resolution_clock::now();
    AProperty<high_resolution_clock::time_point> mCurrentTime;
    AProperty<high_resolution_clock::duration> mDuration = 30s;

    APropertyPrecomputed<high_resolution_clock::duration> mElapsedTime = [&] {
examples/app/game_of_life/src/main.cpp

Game of Life — Game of Life implementation that uses advanced large dynamic data rendering techniques such as ITexture, AImage to be GPU friendly. The computation is performed in AThreadPool.

    AProperty<bool> isRunning = false;
    emits<> frameComplete;

private:
    _<ATimer> mTimer = _new<ATimer>(100ms);
    AFuture<> mFrame;
    glm::ivec2 mSize {};
    AVector<CellState> mStorage;
    AVector<CellState> mNextPopulation;