Boost Timer in Qt Application



  • I'm trying to create qt application that uses a timer from boost, I have a problem with the following code Compiled on osx, qt 5.2, boost 1.55

    This is my code:

    @CCTimer::CCTimer(int interval)
    : m_pingTimer(m_ioServiceTimer, boost::posix_time::seconds(interval)), m_interval(interval)
    {
    m_isRunning = false;
    }

    CCTimer::~CCTimer()
    {
    Stop();
    }

    void CCTimer::Start()
    {
    if(!m_isRunning)
    {
    m_isRunning = true;
    Reset(m_interval);
    m_timerThread = boost::shared_ptrboost::thread(new boost::thread(boost::bind(&boost::asio::io_service::run, &m_ioServiceTimer)));
    }
    }

    void CCTimer::Reset(int durationTime)
    {
    m_beforeTime = boost::posix_time::microsec_clock::universal_time();
    m_pingTimer.expires_from_now(boost::posix_time::seconds(durationTime));
    m_pingTimer.async_wait(boost::bind(&CCTimer::Wait, this, _1));
    }

    void CCTimer::Wait(const boost::system::error_code& errorCode)
    {
    boost::posix_time::time_duration duration = boost::posix_time::microsec_clock::universal_time() - m_beforeTime;
    std::cout << boost::posix_time::microsec_clock::universal_time() << std::endl;
    if(duration.seconds() > m_interval) {
    std::cout << "Error time " << std::endl;
    }
    Reset(m_interval);
    }

    void CCTimer::Stop()
    {
    if(m_isRunning)
    {
    m_isRunning = false;
    m_pingTimer.cancel();
    m_ioServiceTimer.stop();
    m_timerThread->join();
    m_ioServiceTimer.reset();
    }
    }

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("main.qml"));
    viewer.showExpanded();

    CCTimer timer(1);
    timer.Start();
    
    return app.exec();
    

    }@
    After creation of the timer in qt application, console shows:

    @2014-Mar-26 22:04:30.549722
    2014-Mar-26 22:04:31.550977
    2014-Mar-26 22:04:32.552229
    2014-Mar-26 22:04:33.553467
    2014-Mar-26 22:04:34.554734
    2014-Mar-26 22:04:43.684300
    Error time
    2014-Mar-26 22:04:54.694440
    Error time
    2014-Mar-26 22:05:05.694371
    Error time
    2014-Mar-26 22:05:11.669329
    Error time @

    what can be wrong ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.