Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;
- }
- On the first timer run call timer.StartAppTimer(1,10); i am getting the handler message but when i rerun the start time i says started but after 10 seconds i receive no handler message..
- actual Result:
- before timer construction
- TIMER ID : 1
- IO Service started
- 2013-Aug-09 18:12:16.463711 Handler1: expired.
- fsm RECIEVE msgBuf : 1
- before starting timer 2
- TIMER ID : 1
- IO Service started
- END OF MAIN
- Expected result:
- before timer construction
- TIMER ID : 1
- IO Service started
- 2013-Aug-09 18:12:16.463711 Handler1: expired.
- fsm RECIEVE msgBuf : 1
- before starting timer 2
- TIMER ID : 1
- IO Service started
- 2013-Aug-09 (some time) Handler1: expired.
- END OF MAIN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement