带升压的简单计时器机制 - 问题[运行机制的重新朗读不响应处理程序消息]

时间:2013-08-09 12:57:13

标签: c++ boost boost-asio

对C ++和boost库来说非常新 我构建了一个简单的计时器机制 我的问题:     在第一个定时器运行调用timer.StartAppTimer(1,10);我收到处理程序消息 但是当我重新运行启动计时器时它说开始但是在10秒后我没有收到任何处理程序消息.. 请指导

以下是我的代码:

#include <iostream>
#include <windows.h>
#include <timer.h>
#define sleep(n) Sleep(1000 * n)

using namespace boost::posix_time;
using namespace std;


void handler1(int id,const boost::system::error_code &ec)
{
    if (ec == boost::asio::error::operation_aborted)
    {
        std::cout << microsec_clock::local_time() << " Handler1: Timer 1 was cancelled or retriggered." << std::endl;
        std::cout << "TIMER ID : "  << id<< std::endl;
    }
    else
    {
        std::cout << microsec_clock::local_time() << " Handler1: expired." << std::endl;
       std::cout << "fsm RECIEVE msgBuf : "  << id<< std::endl;
    }
}



Mytimer::Mytimer()
  : m_pTimer(NULL),
    m_pThread(NULL)
{

        m_pTimer = new boost::asio::deadline_timer(io_service1);

}

void Mytimer::runTimerThread()
{
        std::cout << "IO Service started "  << std::endl;
        io_service1.run();
}

void Mytimer::startTimer(int time,int timerId)
{
        m_pTimer->expires_from_now(boost::posix_time::seconds(time));
        m_pTimer->async_wait(boost::bind(handler1,timerId,boost::asio::placeholders::error));
        m_pThread = new boost::thread(&Mytimer::runTimerThread, this);
}


void Mytimer::stopTimer()
{
        io_service1.reset();
        io_service1.stop();
}


bool Mytimer::isRunning()
{

    time_duration td = m_pTimer->expires_from_now();
    if (td.total_seconds() > 0)
    {
        return true;
    }
    return false;
}




void TimerAccess::StartAppTimer(int Timerid,int TimerPeriod){
         std::cout << "TIMER ID : "  << Timerid<< std::endl;
        if (Timerid == APPTIMER1){
                timer1.startTimer(TimerPeriod,Timerid);
    }
    if (Timerid == APPTIMER2){
        timer2.startTimer(TimerPeriod,Timerid);
    }
    if (Timerid == APPTIMER3){
        timer3.startTimer(TimerPeriod,Timerid);
    }
}

void TimerAccess::StopTimer(int Timerid){
    if (Timerid == APPTIMER1){
        timer1.stopTimer();
    }
    if (Timerid == APPTIMER2){
        timer2.stopTimer();
    }
    if (Timerid == APPTIMER3){
        timer3.stopTimer();
    }
   // return -1;
}

int main()
{

        cout << " before timer construction" << endl;
         TimerAccess timer;
         timer.StartAppTimer(1,10);
    sleep(15);
    cout << " before starting timer 2" << endl;
    timer.StartAppTimer(1,10);
    sleep(30);
    cout  << " END OF MAIN " << endl;

}

0 个答案:

没有答案