1 | // |
---|
2 | // Task.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/src/Task.cpp#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Tasks |
---|
8 | // Module: Tasks |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include "Poco/Task.h" |
---|
38 | #include "Poco/TaskManager.h" |
---|
39 | #include "Poco/Exception.h" |
---|
40 | |
---|
41 | |
---|
42 | namespace Poco { |
---|
43 | |
---|
44 | |
---|
45 | Task::Task(const std::string& name): |
---|
46 | _name(name), |
---|
47 | _pOwner(0), |
---|
48 | _progress(0), |
---|
49 | _state(TASK_IDLE), |
---|
50 | _cancelEvent(false) |
---|
51 | { |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | Task::~Task() |
---|
56 | { |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | void Task::cancel() |
---|
61 | { |
---|
62 | _state = TASK_CANCELLING; |
---|
63 | _cancelEvent.set(); |
---|
64 | if (_pOwner) |
---|
65 | _pOwner->taskCancelled(this); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | void Task::reset() |
---|
70 | { |
---|
71 | _progress = 0.0; |
---|
72 | _state = TASK_IDLE; |
---|
73 | _cancelEvent.reset(); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | void Task::run() |
---|
78 | { |
---|
79 | TaskManager* pOwner = getOwner(); |
---|
80 | if (pOwner) |
---|
81 | pOwner->taskStarted(this); |
---|
82 | try |
---|
83 | { |
---|
84 | _state = TASK_RUNNING; |
---|
85 | runTask(); |
---|
86 | } |
---|
87 | catch (Exception& exc) |
---|
88 | { |
---|
89 | if (pOwner) |
---|
90 | pOwner->taskFailed(this, exc); |
---|
91 | } |
---|
92 | catch (std::exception& exc) |
---|
93 | { |
---|
94 | if (pOwner) |
---|
95 | pOwner->taskFailed(this, SystemException(exc.what())); |
---|
96 | } |
---|
97 | catch (...) |
---|
98 | { |
---|
99 | if (pOwner) |
---|
100 | pOwner->taskFailed(this, SystemException("unknown exception")); |
---|
101 | } |
---|
102 | _state = TASK_FINISHED; |
---|
103 | if (pOwner) |
---|
104 | pOwner->taskFinished(this); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | bool Task::sleep(long milliseconds) |
---|
109 | { |
---|
110 | return _cancelEvent.tryWait(milliseconds); |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | void Task::setProgress(float progress) |
---|
115 | { |
---|
116 | FastMutex::ScopedLock lock(_mutex); |
---|
117 | |
---|
118 | _progress = progress; |
---|
119 | if (_pOwner) |
---|
120 | _pOwner->taskProgress(this, _progress); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | void Task::setOwner(TaskManager* pOwner) |
---|
125 | { |
---|
126 | FastMutex::ScopedLock lock(_mutex); |
---|
127 | |
---|
128 | _pOwner = pOwner; |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | void Task::setState(TaskState state) |
---|
133 | { |
---|
134 | _state = state; |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | void Task::postNotification(Notification* pNf) |
---|
139 | { |
---|
140 | poco_check_ptr (pNf); |
---|
141 | |
---|
142 | FastMutex::ScopedLock lock(_mutex); |
---|
143 | |
---|
144 | if (_pOwner) |
---|
145 | { |
---|
146 | _pOwner->postNotification(pNf); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | } // namespace Poco |
---|