prevent re-schedule of cancelled TimerTask

This commit is contained in:
Guenter Obiltschnig 2014-11-24 18:38:05 +01:00
parent 42c814cca4
commit c2edf4470f
3 changed files with 21 additions and 2 deletions

View File

@ -154,7 +154,8 @@ public:
protected:
void run();
static void validateTask(const TimerTask::Ptr& pTask);
private:
Timer(const Timer&);
Timer& operator = (const Timer&);

View File

@ -50,7 +50,10 @@ public:
/// not yet run, or has not yet been scheduled, it will never run.
/// If the task has been scheduled for repeated execution, it will never
/// run again. If the task is running when this call occurs, the task
/// will run to completion, but will never run again.
/// will run to completion, but will never run again.
///
/// Warning: A TimerTask that has been cancelled must not be scheduled again.
/// An attempt to do so results in a Poco::Util::IllegalStateException being thrown.
bool isCancelled() const;
/// Returns true iff the TimerTask has been cancelled by a call

View File

@ -257,12 +257,14 @@ void Timer::cancel(bool wait)
void Timer::schedule(TimerTask::Ptr pTask, Poco::Timestamp time)
{
validateTask(pTask);
_queue.enqueueNotification(new TaskNotification(_queue, pTask), time);
}
void Timer::schedule(TimerTask::Ptr pTask, Poco::Clock clock)
{
validateTask(pTask);
_queue.enqueueNotification(new TaskNotification(_queue, pTask), clock);
}
@ -277,12 +279,14 @@ void Timer::schedule(TimerTask::Ptr pTask, long delay, long interval)
void Timer::schedule(TimerTask::Ptr pTask, Poco::Timestamp time, long interval)
{
validateTask(pTask);
_queue.enqueueNotification(new PeriodicTaskNotification(_queue, pTask, interval), time);
}
void Timer::schedule(TimerTask::Ptr pTask, Poco::Clock clock, long interval)
{
validateTask(pTask);
_queue.enqueueNotification(new PeriodicTaskNotification(_queue, pTask, interval), clock);
}
@ -297,6 +301,7 @@ void Timer::scheduleAtFixedRate(TimerTask::Ptr pTask, long delay, long interval)
void Timer::scheduleAtFixedRate(TimerTask::Ptr pTask, Poco::Timestamp time, long interval)
{
validateTask(pTask);
Poco::Timestamp tsNow;
Poco::Clock clock;
Poco::Timestamp::TimeDiff diff = time - tsNow;
@ -307,6 +312,7 @@ void Timer::scheduleAtFixedRate(TimerTask::Ptr pTask, Poco::Timestamp time, long
void Timer::scheduleAtFixedRate(TimerTask::Ptr pTask, Poco::Clock clock, long interval)
{
validateTask(pTask);
_queue.enqueueNotification(new FixedRateTaskNotification(_queue, pTask, interval, clock), clock);
}
@ -322,4 +328,13 @@ void Timer::run()
}
void Timer::validateTask(const TimerTask::Ptr& pTask)
{
if (pTask->isCancelled())
{
throw Poco::IllegalStateException("A cancelled task must not be rescheduled");
}
}
} } // namespace Poco::Util