Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Boost Timer in Qt Application
Forum Update on Monday, May 27th 2025

Boost Timer in Qt Application

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 822 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    error09
    wrote on 26 Mar 2014, 22:34 last edited by
    #1

    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 ?

    1 Reply Last reply
    0

    1/1

    26 Mar 2014, 22:34

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved