MeVisLab Toolbox Reference
|
Base class for a task that is started in an extra thread. More...
#include <mlBackgroundTask.h>
Classes | |
class | Observer |
Abstract interface to observe the BackgroundTask. More... | |
Public Types | |
enum | Status { NotInTaskManager , Queued , Running , Finished , Canceled , Suspended } |
Defines current status of the task. More... | |
Public Member Functions | |
BackgroundTask (void *owner) | |
Creates a new task with given owner . More... | |
virtual | ~BackgroundTask () |
Methods to reimplement | |
virtual void | run ()=0 |
Method that needs to be reimplemented. This is called from the worker thread. More... | |
Setup of task properties | |
bool | isSynchronous () const |
Checks whether the task is synchronous, which means it is not executed in a different thread. More... | |
void | setSynchronous (bool flag) |
Sets whether the task is handled synchronously. Call this only in the setup of the task, not while it is already running. More... | |
void | setSynchronousCancelField (NotifyField *field) |
Sets a cancel field that is used in synchronous mode to check whether cancel was pressed. More... | |
Status handling / canceling | |
Status | status () const |
Returns the status of the task. More... | |
void | cancel () |
Cancels the task (i.e., it sets status to canceled). More... | |
bool | isCanceled () const |
Checks whether the task was canceled. More... | |
bool | shouldStop () |
Returns whether the task should stop its work because it has been canceled. More... | |
void | resume () |
Resumes the task after it was suspended. More... | |
void | suspend () |
Suspends the task. More... | |
std::string | statusString () const |
Returns current status string. More... | |
virtual void | setStatusString (const std::string &status) |
Updates the status string while processing. More... | |
float | progress () const |
Returns current progress This is thread-safe. More... | |
virtual void | setProgress (float progress) |
Updates the progress while processing. More... | |
Owner related methods | |
void | setOwnerWasDeleted () |
Tells the task that its owner was deleted and cancels the thread. More... | |
bool | hasOwner () const |
Returns whether the owner was deleted. More... | |
void * | owner () const |
Returns the owner of the task Call this from GUI thread only! More... | |
Message handling | |
void | waitAndExecuteNextMessage () |
Executes the next message. More... | |
bool | executeNextMessage () |
Executes the next message. More... | |
void | sendMessageToGUI (BackgroundTaskMessage *message) |
Sends the message to the GUI. More... | |
Logging | |
void | logMessage (const std::string &message) |
Logs a message. More... | |
bool | isVerbose () const |
Checks whether task uses verbose logging. More... | |
void | setVerbose (bool flag) |
Sets whether task uses verbose logging. More... | |
Misc | |
double | runningTime () const |
Returns the running time in seconds of the task. More... | |
double | idleTime () const |
Returns the idle time in seconds of the task (e.g., the time it waited for getTile to complete). More... | |
void | getStatusInformation (BackgroundTaskStatusInformation &info) |
Returns the complete status information. More... | |
MLuint32 | taskId () const |
Returns the unique id of this task. The id of a task never changes. More... | |
Observing a task | |
void | setTaskFinishedCB (BackgroundTaskFinishedCB *callback, void *userdata) |
Set this directly after the initialization of the task from the GUI thread. More... | |
void | addObserver (Observer *observer) |
Adds the given observer. More... | |
void | removeObserver (Observer *observer) |
Removes the given observer. More... | |
Protected Member Functions | |
void | sendStatusChangedToObservers () |
Sends the status change to all observers. More... | |
void | sendStatusStringChangedToObservers () |
Sends the status string change to all observers. More... | |
void | sendLogMessageToObservers (const std::string &msg) |
Sends the status string change to all observers. More... | |
void | addIdleTime (double timeInSecs) |
Adds given timeInSecs as spent idle time. More... | |
Friends | |
class | BackgroundTaskManager |
struct | BackgroundTaskManagerWorkerFunc |
Base class for a task that is started in an extra thread.
Reimplement the virtual run() method to implement processing that is done in the task. A task can be scheduled using BackgroundTaskManager::self()->runTask(task).
Definition at line 41 of file mlBackgroundTask.h.
Defines current status of the task.
Enumerator | |
---|---|
NotInTaskManager | |
Queued | |
Running | |
Finished | |
Canceled | |
Suspended |
Definition at line 47 of file mlBackgroundTask.h.
ml::BackgroundTask::BackgroundTask | ( | void * | owner | ) |
Creates a new task with given owner
.
The owner can be used to associate the task with a given class instance, e.g., an ml::Module. The task never accesses the owner itself.
|
virtual |
|
inlineprotected |
Adds given timeInSecs
as spent idle time.
Definition at line 271 of file mlBackgroundTask.h.
void ml::BackgroundTask::addObserver | ( | Observer * | observer | ) |
Adds the given observer.
This is thread-safe and the ownership stays with caller.
void ml::BackgroundTask::cancel | ( | ) |
Cancels the task (i.e., it sets status to canceled).
This is thread-safe and typically called from the GUI.
bool ml::BackgroundTask::executeNextMessage | ( | ) |
Executes the next message.
It does not block if no message is available. It returns true
if there was a message to process. Note: only call this from within run().
void ml::BackgroundTask::getStatusInformation | ( | BackgroundTaskStatusInformation & | info | ) |
Returns the complete status information.
This is thread-safe.
bool ml::BackgroundTask::hasOwner | ( | ) | const |
Returns whether the owner was deleted.
Call this from GUI thread only!
|
inline |
Returns the idle time in seconds of the task (e.g., the time it waited for getTile to complete).
After it has finished or been canceled, only call this from the GUI thread.
Definition at line 210 of file mlBackgroundTask.h.
bool ml::BackgroundTask::isCanceled | ( | ) | const |
Checks whether the task was canceled.
This is thread-safe and typically called from the GUI.
|
inline |
Checks whether the task is synchronous, which means it is not executed in a different thread.
Definition at line 77 of file mlBackgroundTask.h.
|
inline |
Checks whether task uses verbose logging.
Definition at line 191 of file mlBackgroundTask.h.
void ml::BackgroundTask::logMessage | ( | const std::string & | message | ) |
Logs a message.
It is only logged if verbose logging is turned on. This is thread-safe.
void* ml::BackgroundTask::owner | ( | ) | const |
Returns the owner of the task Call this from GUI thread only!
float ml::BackgroundTask::progress | ( | ) | const |
Returns current progress This is thread-safe.
void ml::BackgroundTask::removeObserver | ( | Observer * | observer | ) |
Removes the given observer.
This is thread-safe and the ownership stays with caller.
void ml::BackgroundTask::resume | ( | ) |
Resumes the task after it was suspended.
It will only do something if the current status() returns 'Suspended'. This is thread-safe.
|
pure virtual |
Method that needs to be reimplemented. This is called from the worker thread.
Implemented in ml::ProcessAllPagesBackgroundTask.
|
inline |
Returns the running time in seconds of the task.
After it has finished or been canceled, only call this from the GUI thread.
Definition at line 206 of file mlBackgroundTask.h.
|
protected |
Sends the status string change to all observers.
void ml::BackgroundTask::sendMessageToGUI | ( | BackgroundTaskMessage * | message | ) |
Sends the message to the GUI.
The ownership of the message is passed to the method. It sets the sender of the message to this BackgroundTask. Note: only call this from within run().
Referenced by ml::MessagingBackgroundTask::callMethodOnGUI().
|
protected |
Sends the status change to all observers.
|
protected |
Sends the status string change to all observers.
void ml::BackgroundTask::setOwnerWasDeleted | ( | ) |
Tells the task that its owner was deleted and cancels the thread.
Call this from GUI thread only!
|
virtual |
Updates the progress while processing.
Call this from run() method only.
Reimplemented in ml::ModuleBackgroundTask.
|
virtual |
Updates the status string while processing.
Call this from the run() method only.
Reimplemented in ml::ModuleBackgroundTask.
|
inline |
Sets whether the task is handled synchronously. Call this only in the setup of the task, not while it is already running.
Definition at line 79 of file mlBackgroundTask.h.
|
inline |
Sets a cancel field that is used in synchronous mode to check whether cancel was pressed.
Definition at line 84 of file mlBackgroundTask.h.
void ml::BackgroundTask::setTaskFinishedCB | ( | BackgroundTaskFinishedCB * | callback, |
void * | userdata | ||
) |
Set this directly after the initialization of the task from the GUI thread.
This callback will be called whenever a task is either finished or canceled.
|
inline |
Sets whether task uses verbose logging.
Call this only in the setup of the task, not while it is already running.
Definition at line 194 of file mlBackgroundTask.h.
bool ml::BackgroundTask::shouldStop | ( | ) |
Returns whether the task should stop its work because it has been canceled.
Note: This method should be called regularly from the implementation of run() to allow that a task stops/is suspended when the user presses/suspend cancel. If it returns true, the task should clean up and return from run() ASAP. This method may only be called from the run() method and only from that thread, as it will block when the task is suspended.
Status ml::BackgroundTask::status | ( | ) | const |
Returns the status of the task.
This is thread-safe.
std::string ml::BackgroundTask::statusString | ( | ) | const |
Returns current status string.
This is thread-safe.
void ml::BackgroundTask::suspend | ( | ) |
Suspends the task.
This way, it will wait until resume() is called. This is thread-safe. To make this feature work, the task's run() implementation needs to call shouldStop() regularly.
|
inline |
Returns the unique id of this task. The id of a task never changes.
Definition at line 217 of file mlBackgroundTask.h.
void ml::BackgroundTask::waitAndExecuteNextMessage | ( | ) |
Executes the next message.
It waits if no messages are available and processes the first message that is received. Note: only call this from within run().
|
friend |
Definition at line 274 of file mlBackgroundTask.h.
|
friend |
Definition at line 275 of file mlBackgroundTask.h.