Task.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // Task.h
  3. //
  4. // Library: Foundation
  5. // Package: Tasks
  6. // Module: Tasks
  7. //
  8. // Definition of the Task class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_Task_INCLUDED
  16. #define Foundation_Task_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Runnable.h"
  19. #include "Poco/RefCountedObject.h"
  20. #include "Poco/Mutex.h"
  21. #include "Poco/Event.h"
  22. namespace Poco {
  23. class TaskManager;
  24. class Notification;
  25. class NotificationCenter;
  26. class Foundation_API Task: public Runnable, public RefCountedObject
  27. /// A Task is a subclass of Runnable that has a name
  28. /// and supports progress reporting and cancellation.
  29. ///
  30. /// A TaskManager object can be used to take care of the
  31. /// lifecycle of a Task.
  32. {
  33. public:
  34. enum TaskState
  35. {
  36. TASK_IDLE,
  37. TASK_STARTING,
  38. TASK_RUNNING,
  39. TASK_CANCELLING,
  40. TASK_FINISHED
  41. };
  42. Task(const std::string& name);
  43. /// Creates the Task.
  44. const std::string& name() const;
  45. /// Returns the task's name.
  46. float progress() const;
  47. /// Returns the task's progress.
  48. /// The value will be between 0.0 (just started)
  49. /// and 1.0 (completed).
  50. virtual void cancel();
  51. /// Requests the task to cancel itself. For cancellation
  52. /// to work, the task's runTask() method must periodically
  53. /// call isCancelled() and react accordingly.
  54. ///
  55. /// Can be overridden to implement custom behavior,
  56. /// but the base class implementation of cancel() should
  57. /// be called to ensure proper behavior.
  58. bool isCancelled() const;
  59. /// Returns true if cancellation of the task has been
  60. /// requested.
  61. ///
  62. /// A Task's runTask() method should periodically
  63. /// call this method and stop whatever it is doing in an
  64. /// orderly way when this method returns true.
  65. TaskState state() const;
  66. /// Returns the task's current state.
  67. void reset();
  68. /// Sets the task's progress to zero and clears the
  69. /// cancel flag.
  70. virtual void runTask() = 0;
  71. /// Do whatever the task needs to do. Must
  72. /// be overridden by subclasses.
  73. void run();
  74. /// Calls the task's runTask() method and notifies the owner
  75. /// of the task's start and completion.
  76. protected:
  77. bool sleep(long milliseconds);
  78. /// Suspends the current thread for the specified
  79. /// amount of time.
  80. ///
  81. /// If the task is cancelled while it is sleeping,
  82. /// sleep() will return immediately and the return
  83. /// value will be true. If the time interval
  84. /// passes without the task being cancelled, the
  85. /// return value is false.
  86. ///
  87. /// A Task should use this method in favor of Thread::sleep().
  88. void setProgress(float progress);
  89. /// Sets the task's progress.
  90. /// The value should be between 0.0 (just started)
  91. /// and 1.0 (completed).
  92. virtual void postNotification(Notification* pNf);
  93. /// Posts a notification to the task manager's
  94. /// notification center.
  95. ///
  96. /// A task can use this method to post custom
  97. /// notifications about its progress.
  98. void setOwner(TaskManager* pOwner);
  99. /// Sets the (optional) owner of the task.
  100. TaskManager* getOwner() const;
  101. /// Returns the owner of the task, which may be NULL.
  102. void setState(TaskState state);
  103. /// Sets the task's state.
  104. virtual ~Task();
  105. /// Destroys the Task.
  106. private:
  107. Task();
  108. Task(const Task&);
  109. Task& operator = (const Task&);
  110. std::string _name;
  111. TaskManager* _pOwner;
  112. float _progress;
  113. TaskState _state;
  114. Event _cancelEvent;
  115. mutable FastMutex _mutex;
  116. friend class TaskManager;
  117. };
  118. //
  119. // inlines
  120. //
  121. inline const std::string& Task::name() const
  122. {
  123. return _name;
  124. }
  125. inline float Task::progress() const
  126. {
  127. FastMutex::ScopedLock lock(_mutex);
  128. return _progress;
  129. }
  130. inline bool Task::isCancelled() const
  131. {
  132. return _state == TASK_CANCELLING;
  133. }
  134. inline Task::TaskState Task::state() const
  135. {
  136. return _state;
  137. }
  138. inline TaskManager* Task::getOwner() const
  139. {
  140. FastMutex::ScopedLock lock(_mutex);
  141. return _pOwner;
  142. }
  143. } // namespace Poco
  144. #endif // Foundation_Task_INCLUDED