1 | // |
---|
2 | // Timer.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Util/include/Poco/Util/Timer.h#2 $ |
---|
5 | // |
---|
6 | // Library: Util |
---|
7 | // Package: Timer |
---|
8 | // Module: Timer |
---|
9 | // |
---|
10 | // Definition of the Timer class. |
---|
11 | // |
---|
12 | // Copyright (c) 2009, Applied Informatics Software Engineering GmbH. |
---|
13 | // and Contributors. |
---|
14 | // |
---|
15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
20 | // do so, all subject to the following: |
---|
21 | // |
---|
22 | // The copyright notices in the Software and this entire statement, including |
---|
23 | // the above license grant, this restriction and the following disclaimer, |
---|
24 | // must be included in all copies of the Software, in whole or in part, and |
---|
25 | // all derivative works of the Software, unless such copies or derivative |
---|
26 | // works are solely in the form of machine-executable object code generated by |
---|
27 | // a source language processor. |
---|
28 | // |
---|
29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
35 | // DEALINGS IN THE SOFTWARE. |
---|
36 | // |
---|
37 | |
---|
38 | |
---|
39 | #ifndef Util_Timer_INCLUDED |
---|
40 | #define Util_Timer_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Util/Util.h" |
---|
44 | #include "Poco/Util/TimerTask.h" |
---|
45 | #include "Poco/TimedNotificationQueue.h" |
---|
46 | #include "Poco/Thread.h" |
---|
47 | #include "Poco/Runnable.h" |
---|
48 | |
---|
49 | |
---|
50 | namespace Poco { |
---|
51 | namespace Util { |
---|
52 | |
---|
53 | |
---|
54 | class Util_API Timer: protected Poco::Runnable |
---|
55 | /// A Timer allows to schedule tasks (TimerTask objects) for future execution |
---|
56 | /// in a background thread. Tasks may be scheduled for one-time execution, |
---|
57 | /// or for repeated execution at regular intervals. |
---|
58 | /// |
---|
59 | /// The Timer object creates a thread that executes all scheduled tasks |
---|
60 | /// sequentially. Therefore, tasks should complete their work as quickly |
---|
61 | /// as possible, otherwise subsequent tasks may be delayed. |
---|
62 | /// |
---|
63 | /// Timer is save for multithreaded use - multiple threads can schedule |
---|
64 | /// new tasks simultaneously. |
---|
65 | /// |
---|
66 | /// Acknowledgement: The interface of this class has been inspired by |
---|
67 | /// the java.util.Timer class from Java 1.3. |
---|
68 | { |
---|
69 | public: |
---|
70 | Timer(); |
---|
71 | /// Creates the Timer. |
---|
72 | |
---|
73 | explicit Timer(Poco::Thread::Priority priority); |
---|
74 | /// Creates the Timer, using a timer thread with |
---|
75 | /// the given priority. |
---|
76 | |
---|
77 | ~Timer(); |
---|
78 | /// Destroys the Timer, cancelling all pending tasks. |
---|
79 | |
---|
80 | void cancel(); |
---|
81 | /// Cancels all pending tasks. |
---|
82 | /// |
---|
83 | /// If a task is currently running, it is allowed to finish. |
---|
84 | |
---|
85 | void schedule(TimerTask::Ptr pTask, Poco::Timestamp time); |
---|
86 | /// Schedules a task for execution at the specified time. |
---|
87 | /// |
---|
88 | /// If the time lies in the past, the task is executed |
---|
89 | /// immediately. |
---|
90 | |
---|
91 | void schedule(TimerTask::Ptr pTask, long delay, long interval); |
---|
92 | /// Schedules a task for periodic execution. |
---|
93 | /// |
---|
94 | /// The task is first executed after the given delay. |
---|
95 | /// Subsequently, the task is executed periodically with |
---|
96 | /// the given interval in milliseconds between invocations. |
---|
97 | |
---|
98 | void schedule(TimerTask::Ptr pTask, Poco::Timestamp time, long interval); |
---|
99 | /// Schedules a task for periodic execution. |
---|
100 | /// |
---|
101 | /// The task is first executed at the given time. |
---|
102 | /// Subsequently, the task is executed periodically with |
---|
103 | /// the given interval in milliseconds between invocations. |
---|
104 | |
---|
105 | void scheduleAtFixedRate(TimerTask::Ptr pTask, long delay, long interval); |
---|
106 | /// Schedules a task for periodic execution at a fixed rate. |
---|
107 | /// |
---|
108 | /// The task is first executed after the given delay. |
---|
109 | /// Subsequently, the task is executed periodically |
---|
110 | /// every number of milliseconds specified by interval. |
---|
111 | /// |
---|
112 | /// If task execution takes longer than the given interval, |
---|
113 | /// further executions are delayed. |
---|
114 | |
---|
115 | void scheduleAtFixedRate(TimerTask::Ptr pTask, Poco::Timestamp time, long interval); |
---|
116 | /// Schedules a task for periodic execution at a fixed rate. |
---|
117 | /// |
---|
118 | /// The task is first executed at the given time. |
---|
119 | /// Subsequently, the task is executed periodically |
---|
120 | /// every number of milliseconds specified by interval. |
---|
121 | /// |
---|
122 | /// If task execution takes longer than the given interval, |
---|
123 | /// further executions are delayed. |
---|
124 | |
---|
125 | protected: |
---|
126 | void run(); |
---|
127 | |
---|
128 | private: |
---|
129 | Timer(const Timer&); |
---|
130 | Timer& operator = (const Timer&); |
---|
131 | |
---|
132 | Poco::TimedNotificationQueue _queue; |
---|
133 | Poco::Thread _thread; |
---|
134 | }; |
---|
135 | |
---|
136 | |
---|
137 | } } // namespace Poco::Util |
---|
138 | |
---|
139 | |
---|
140 | #endif // Util_Timer_INCLUDED |
---|