diff --git a/src_main/thread/pitimer.cpp b/src_main/thread/pitimer.cpp index 87827bbd..f7005539 100755 --- a/src_main/thread/pitimer.cpp +++ b/src_main/thread/pitimer.cpp @@ -61,14 +61,14 @@ _PITimerBase::_PITimerBase() { void _PITimerBase::setInterval(double i) { interval_ = i; if (isRunning()) { - stop(); + stop(true); start(); } } bool _PITimerBase::start(double interval_ms) { - if (isRunning()) stop(); + if (isRunning()) stop(true); deferred_ = false; setInterval(interval_ms); running_ = startTimer(interval_ms); @@ -77,7 +77,7 @@ bool _PITimerBase::start(double interval_ms) { void _PITimerBase::startDeferred(double interval_ms, PIDateTime start_datetime) { - if (isRunning()) stop(); + if (isRunning()) stop(true); deferred_ = true; deferred_mode = true; deferred_datetime = start_datetime; @@ -87,7 +87,7 @@ void _PITimerBase::startDeferred(double interval_ms, PIDateTime start_datetime) void _PITimerBase::startDeferred(double interval_ms, double delay_ms) { - if (isRunning()) stop(); + if (isRunning()) stop(true); deferred_ = true; deferred_mode = false; deferred_delay = delay_ms; @@ -96,9 +96,9 @@ void _PITimerBase::startDeferred(double interval_ms, double delay_ms) { } -bool _PITimerBase::stop() { +bool _PITimerBase::stop(bool wait) { if (!isRunning()) return true; - running_ = !stopTimer(); + running_ = !stopTimer(wait); return !running_; } @@ -115,7 +115,7 @@ protected: int wait_dt, wait_dd, wait_tick; private: virtual bool startTimer(double interval_ms); - virtual bool stopTimer(); + virtual bool stopTimer(bool wait); static void threadFuncS(void * d) {((_PITimerImp_Thread*)d)->threadFunc();} void adjustTimes(); @@ -133,7 +133,7 @@ public: protected: private: virtual bool startTimer(double interval_ms); - virtual bool stopTimer(); + virtual bool stopTimer(bool wait); int ti; _PITimerImp_RT_Private_ * priv; @@ -144,7 +144,7 @@ private: class _PITimerImp_Pool: public _PITimerImp_Thread { public: _PITimerImp_Pool(); - virtual ~_PITimerImp_Pool() {stop();} + virtual ~_PITimerImp_Pool() {stop(true);} private: class Pool: public PIThread { public: @@ -157,7 +157,7 @@ private: PIVector<_PITimerImp_Pool * > timers, to_remove; }; virtual bool startTimer(double interval_ms); - virtual bool stopTimer(); + virtual bool stopTimer(bool wait); }; @@ -173,7 +173,7 @@ _PITimerImp_Thread::_PITimerImp_Thread() { _PITimerImp_Thread::~_PITimerImp_Thread() { - stop(); + stop(true); } @@ -202,11 +202,12 @@ bool _PITimerImp_Thread::startTimer(double interval_ms) { } -bool _PITimerImp_Thread::stopTimer() { +bool _PITimerImp_Thread::stopTimer(bool wait) { thread_.stop(); - if (!thread_.waitForFinish(10)) - if (thread_.isRunning()) - thread_.terminate(); + if (wait) + if (!thread_.waitForFinish(10)) + if (thread_.isRunning()) + thread_.terminate(); return true; } @@ -311,7 +312,7 @@ _PITimerImp_RT::_PITimerImp_RT() { _PITimerImp_RT::~_PITimerImp_RT() { - stop(); + stop(true); delete priv; } @@ -344,7 +345,7 @@ bool _PITimerImp_RT::startTimer(double interval_ms) { } -bool _PITimerImp_RT::stopTimer() { +bool _PITimerImp_RT::stopTimer(bool wait) { if (ti < 0) return true; timer_delete(priv->tt); ti = -1; @@ -425,7 +426,7 @@ bool _PITimerImp_Pool::startTimer(double interval_ms) { } -bool _PITimerImp_Pool::stopTimer() { +bool _PITimerImp_Pool::stopTimer(bool wait) { Pool::instance()->remove(this); return true; } diff --git a/src_main/thread/pitimer.h b/src_main/thread/pitimer.h index 9ada636b..d2deeb5e 100755 --- a/src_main/thread/pitimer.h +++ b/src_main/thread/pitimer.h @@ -35,7 +35,7 @@ class _PITimerBase { friend class PITimer; public: _PITimerBase(); - virtual ~_PITimerBase() {stop();} + virtual ~_PITimerBase() {stop(true);} double interval() const {return interval_;} void setInterval(double i); @@ -52,7 +52,7 @@ public: void startDeferred(PIDateTime start_datetime) {startDeferred(interval_, start_datetime);} void startDeferred(double interval_ms, PIDateTime start_datetime); - bool stop(); + bool stop(bool wait); typedef void(*TickFunc)(PITimer*); TickFunc tfunc; @@ -61,7 +61,7 @@ public: protected: virtual bool startTimer(double interval_ms) = 0; - virtual bool stopTimer() = 0; + virtual bool stopTimer(bool wait) = 0; double interval_, deferred_delay; bool deferred_, deferred_mode; // mode: true - date, false - delay @@ -117,7 +117,7 @@ public: EVENT_HANDLER0(bool, start) {return imp->start();} EVENT_HANDLER1(bool, start, double, interval_ms_d) {setInterval(interval_ms_d); return imp->start(interval_ms_d);} bool start(int interval_ms_i) {setInterval(double(interval_ms_i)); return imp->start(double(interval_ms_i));} - EVENT_HANDLER0(bool, restart) {imp->stop(); return imp->start();} + EVENT_HANDLER0(bool, restart) {imp->stop(true); return imp->start();} /** \brief Start timer with \b interval() loop delay after \b delay_msecs delay. @@ -140,7 +140,8 @@ public: * \b interval_msecs loop delay. */ void startDeferred(double interval_ms, PIDateTime start_datetime) {imp->startDeferred(interval_ms, start_datetime);} - EVENT_HANDLER0(bool, stop) {return imp->stop();} + EVENT_HANDLER0(bool, stop) {return imp->stop(true);} + EVENT_HANDLER1(bool, stop, bool, wait) {return imp->stop(wait);} bool waitForFinish() {return waitForFinish(-1);} bool waitForFinish(int timeout_msecs); @@ -194,8 +195,8 @@ public: //! \fn bool restart() //! \brief Stop and start timer with \a interval() loop delay - //! \fn bool stop() - //! \brief Stop timer + //! \fn bool stop(bool wait = true) + //! \brief Stop timer and wait for it finish if "wait" //! \fn void clearDelimiters() //! \brief Remove all frequency delimiters