TimerTask.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // TimerTask.h
  3. //
  4. // Library: Util
  5. // Package: Timer
  6. // Module: TimerTask
  7. //
  8. // Definition of the TimerTask class.
  9. //
  10. // Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Util_TimerTask_INCLUDED
  16. #define Util_TimerTask_INCLUDED
  17. #include "Poco/Util/Util.h"
  18. #include "Poco/Runnable.h"
  19. #include "Poco/RefCountedObject.h"
  20. #include "Poco/AutoPtr.h"
  21. #include "Poco/Timestamp.h"
  22. namespace Poco {
  23. namespace Util {
  24. class Util_API TimerTask: public Poco::RefCountedObject, public Poco::Runnable
  25. /// A task that can be scheduled for one-time or
  26. /// repeated execution by a Timer.
  27. ///
  28. /// This is an abstract class. Subclasses must override the run() member
  29. /// function to implement the actual task logic.
  30. {
  31. public:
  32. typedef Poco::AutoPtr<TimerTask> Ptr;
  33. TimerTask();
  34. /// Creates the TimerTask.
  35. void cancel();
  36. /// Cancels the execution of the timer.
  37. /// If the task has been scheduled for one-time execution and has
  38. /// not yet run, or has not yet been scheduled, it will never run.
  39. /// If the task has been scheduled for repeated execution, it will never
  40. /// run again. If the task is running when this call occurs, the task
  41. /// will run to completion, but will never run again.
  42. ///
  43. /// Warning: A TimerTask that has been cancelled must not be scheduled again.
  44. /// An attempt to do so results in a Poco::Util::IllegalStateException being thrown.
  45. bool isCancelled() const;
  46. /// Returns true iff the TimerTask has been cancelled by a call
  47. /// to cancel().
  48. Poco::Timestamp lastExecution() const;
  49. /// Returns the time of the last execution of the timer task.
  50. ///
  51. /// Returns 0 if the timer has never been executed.
  52. protected:
  53. ~TimerTask();
  54. /// Destroys the TimerTask.
  55. private:
  56. TimerTask(const TimerTask&);
  57. TimerTask& operator = (const TimerTask&);
  58. Poco::Timestamp _lastExecution;
  59. bool _isCancelled;
  60. friend class TaskNotification;
  61. };
  62. //
  63. // inlines
  64. //
  65. inline bool TimerTask::isCancelled() const
  66. {
  67. return _isCancelled;
  68. }
  69. inline Poco::Timestamp TimerTask::lastExecution() const
  70. {
  71. return _lastExecution;
  72. }
  73. } } // namespace Poco::Util
  74. #endif // Util_TimerTask_INCLUDED