mirror of
https://github.com/pocoproject/poco.git
synced 2025-05-29 23:42:39 +02:00
prevent re-schedule of cancelled TimerTask
This commit is contained in:
parent
42c814cca4
commit
c2edf4470f
@ -154,7 +154,8 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void run();
|
void run();
|
||||||
|
static void validateTask(const TimerTask::Ptr& pTask);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Timer(const Timer&);
|
Timer(const Timer&);
|
||||||
Timer& operator = (const Timer&);
|
Timer& operator = (const Timer&);
|
||||||
|
@ -50,7 +50,10 @@ public:
|
|||||||
/// not yet run, or has not yet been scheduled, it will never run.
|
/// 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
|
/// 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
|
/// 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;
|
bool isCancelled() const;
|
||||||
/// Returns true iff the TimerTask has been cancelled by a call
|
/// Returns true iff the TimerTask has been cancelled by a call
|
||||||
|
@ -257,12 +257,14 @@ void Timer::cancel(bool wait)
|
|||||||
|
|
||||||
void Timer::schedule(TimerTask::Ptr pTask, Poco::Timestamp time)
|
void Timer::schedule(TimerTask::Ptr pTask, Poco::Timestamp time)
|
||||||
{
|
{
|
||||||
|
validateTask(pTask);
|
||||||
_queue.enqueueNotification(new TaskNotification(_queue, pTask), time);
|
_queue.enqueueNotification(new TaskNotification(_queue, pTask), time);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Timer::schedule(TimerTask::Ptr pTask, Poco::Clock clock)
|
void Timer::schedule(TimerTask::Ptr pTask, Poco::Clock clock)
|
||||||
{
|
{
|
||||||
|
validateTask(pTask);
|
||||||
_queue.enqueueNotification(new TaskNotification(_queue, pTask), clock);
|
_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)
|
void Timer::schedule(TimerTask::Ptr pTask, Poco::Timestamp time, long interval)
|
||||||
{
|
{
|
||||||
|
validateTask(pTask);
|
||||||
_queue.enqueueNotification(new PeriodicTaskNotification(_queue, pTask, interval), time);
|
_queue.enqueueNotification(new PeriodicTaskNotification(_queue, pTask, interval), time);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Timer::schedule(TimerTask::Ptr pTask, Poco::Clock clock, long interval)
|
void Timer::schedule(TimerTask::Ptr pTask, Poco::Clock clock, long interval)
|
||||||
{
|
{
|
||||||
|
validateTask(pTask);
|
||||||
_queue.enqueueNotification(new PeriodicTaskNotification(_queue, pTask, interval), clock);
|
_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)
|
void Timer::scheduleAtFixedRate(TimerTask::Ptr pTask, Poco::Timestamp time, long interval)
|
||||||
{
|
{
|
||||||
|
validateTask(pTask);
|
||||||
Poco::Timestamp tsNow;
|
Poco::Timestamp tsNow;
|
||||||
Poco::Clock clock;
|
Poco::Clock clock;
|
||||||
Poco::Timestamp::TimeDiff diff = time - tsNow;
|
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)
|
void Timer::scheduleAtFixedRate(TimerTask::Ptr pTask, Poco::Clock clock, long interval)
|
||||||
{
|
{
|
||||||
|
validateTask(pTask);
|
||||||
_queue.enqueueNotification(new FixedRateTaskNotification(_queue, pTask, interval, clock), clock);
|
_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
|
} } // namespace Poco::Util
|
||||||
|
Loading…
x
Reference in New Issue
Block a user