Task.h 4.0 KB

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