MeVisLab Toolbox Reference
ml::BackgroundTask Class Referenceabstract

#include <mlBackgroundTask.h>

Inheritance diagram for ml::BackgroundTask:
ml::MessagingBackgroundTask ml::ImagingBackgroundTask ml::ModuleBackgroundTask ml::ProcessAllPagesBackgroundTask ml::RTObjectSaveBackgroundTask

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)
virtual ~BackgroundTask ()
Methods to reimplement
virtual void run ()=0
 Method that needs to be reimplemented. This is called from the worker thread.
Setup of task properties
bool isSynchronous () const
 Checks whether the task is synchronous, which means it is not executed in a different thread.
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.
void setSynchronousCancelField (NotifyField *field)
 Sets a cancel field that is used in synchronous mode to check whether cancel was pressed.
Status handling / canceling
Status status () const
void cancel ()
bool isCanceled () const
bool shouldStop ()
void resume ()
void suspend ()
std::string statusString () const
virtual void setStatusString (const std::string &status)
float progress () const
virtual void setProgress (float progress)
Owner related methods
void setOwnerWasDeleted ()
bool hasOwner () const
void * owner () const
Message handling
void waitAndExecuteNextMessage ()
bool executeNextMessage ()
void sendMessageToGUI (BackgroundTaskMessage *message)
Logging
void logMessage (const std::string &message)
bool isVerbose () const
 Checks whether task uses verbose logging.
void setVerbose (bool flag)
Misc
double runningTime () const
double idleTime () const
void getStatusInformation (BackgroundTaskStatusInformation &info)
MLuint32 taskId () const
 Returns the unique id of this task. The id of a task never changes.
Observing a task
void setTaskFinishedCB (BackgroundTaskFinishedCB *callback, void *userdata)
void addObserver (Observer *observer)
void removeObserver (Observer *observer)

Protected Member Functions

void sendStatusChangedToObservers ()
 Sends the status change to all observers.
void sendStatusStringChangedToObservers ()
 Sends the status string change to all observers.
void sendLogMessageToObservers (const std::string &msg)
 Sends the status string change to all observers.
void addIdleTime (double timeInSecs)
 Adds given timeInSecs as spent idle time.

Friends

class BackgroundTaskManager
struct BackgroundTaskManagerWorkerFunc

Detailed Description

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.

Member Enumeration Documentation

◆ Status

Defines current status of the task.

Enumerator
NotInTaskManager 
Queued 
Running 
Finished 
Canceled 
Suspended 

Definition at line 47 of file mlBackgroundTask.h.

Constructor & Destructor Documentation

◆ BackgroundTask()

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.

References owner().

Referenced by ml::BackgroundTask::Observer::logMessage(), ml::MessagingBackgroundTask::MessagingBackgroundTask(), ml::BackgroundTask::Observer::statusChanged(), and ml::BackgroundTask::Observer::statusStringChanged().

◆ ~BackgroundTask()

virtual ml::BackgroundTask::~BackgroundTask ( )
virtual

Member Function Documentation

◆ addIdleTime()

void ml::BackgroundTask::addIdleTime ( double timeInSecs)
inlineprotected

Adds given timeInSecs as spent idle time.

Definition at line 271 of file mlBackgroundTask.h.

◆ addObserver()

void ml::BackgroundTask::addObserver ( Observer * observer)

Adds the given observer. This is thread-safe and the ownership stays with caller.

◆ cancel()

void ml::BackgroundTask::cancel ( )

Cancels the task (i.e., it sets status to canceled). This is thread-safe and typically called from the GUI.

◆ executeNextMessage()

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().

◆ getStatusInformation()

void ml::BackgroundTask::getStatusInformation ( BackgroundTaskStatusInformation & info)

Returns the complete status information. This is thread-safe.

◆ hasOwner()

bool ml::BackgroundTask::hasOwner ( ) const

Returns whether the owner was deleted. Call this from GUI thread only!

◆ idleTime()

double ml::BackgroundTask::idleTime ( ) const
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.

◆ isCanceled()

bool ml::BackgroundTask::isCanceled ( ) const

Checks whether the task was canceled. This is thread-safe and typically called from the GUI.

◆ isSynchronous()

bool ml::BackgroundTask::isSynchronous ( ) const
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.

Referenced by ml::RTObjectSaveBackgroundTask::setDicomTree().

◆ isVerbose()

bool ml::BackgroundTask::isVerbose ( ) const
inline

Checks whether task uses verbose logging.

Definition at line 191 of file mlBackgroundTask.h.

◆ logMessage()

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.

◆ owner()

void * ml::BackgroundTask::owner ( ) const

Returns the owner of the task Call this from GUI thread only!

Referenced by BackgroundTask(), and ml::ImagingBackgroundTask::ImagingBackgroundTask().

◆ progress()

float ml::BackgroundTask::progress ( ) const

Returns current progress This is thread-safe.

Referenced by setProgress(), and ml::ModuleBackgroundTask::setProgress().

◆ removeObserver()

void ml::BackgroundTask::removeObserver ( Observer * observer)

Removes the given observer. This is thread-safe and the ownership stays with caller.

◆ resume()

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.

◆ run()

virtual void ml::BackgroundTask::run ( )
pure virtual

Method that needs to be reimplemented. This is called from the worker thread.

Implemented in ml::ProcessAllPagesBackgroundTask, and ml::RTObjectSaveBackgroundTask.

◆ runningTime()

double ml::BackgroundTask::runningTime ( ) const
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.

◆ sendLogMessageToObservers()

void ml::BackgroundTask::sendLogMessageToObservers ( const std::string & msg)
protected

Sends the status string change to all observers.

◆ sendMessageToGUI()

void ml::BackgroundTask::sendMessageToGUI ( BackgroundTaskMessage * message)

◆ sendStatusChangedToObservers()

void ml::BackgroundTask::sendStatusChangedToObservers ( )
protected

Sends the status change to all observers.

◆ sendStatusStringChangedToObservers()

void ml::BackgroundTask::sendStatusStringChangedToObservers ( )
protected

Sends the status string change to all observers.

◆ setOwnerWasDeleted()

void ml::BackgroundTask::setOwnerWasDeleted ( )

Tells the task that its owner was deleted and cancels the thread. Call this from GUI thread only!

◆ setProgress()

virtual void ml::BackgroundTask::setProgress ( float progress)
virtual

Updates the progress while processing. Call this from run() method only.

Reimplemented in ml::ModuleBackgroundTask.

References progress().

◆ setStatusString()

virtual void ml::BackgroundTask::setStatusString ( const std::string & status)
virtual

Updates the status string while processing. Call this from the run() method only.

Reimplemented in ml::ModuleBackgroundTask.

References status().

◆ setSynchronous()

void ml::BackgroundTask::setSynchronous ( bool flag)
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.

◆ setSynchronousCancelField()

void ml::BackgroundTask::setSynchronousCancelField ( NotifyField * field)
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.

◆ setTaskFinishedCB()

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.

◆ setVerbose()

void ml::BackgroundTask::setVerbose ( bool flag)
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.

◆ shouldStop()

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()

◆ statusString()

std::string ml::BackgroundTask::statusString ( ) const

Returns current status string. This is thread-safe.

◆ suspend()

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.

◆ taskId()

MLuint32 ml::BackgroundTask::taskId ( ) const
inline

Returns the unique id of this task. The id of a task never changes.

Definition at line 217 of file mlBackgroundTask.h.

◆ waitAndExecuteNextMessage()

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().

◆ BackgroundTaskManager

friend class BackgroundTaskManager
friend

Definition at line 274 of file mlBackgroundTask.h.

References BackgroundTaskManager.

Referenced by BackgroundTaskManager.

◆ BackgroundTaskManagerWorkerFunc

friend struct BackgroundTaskManagerWorkerFunc
friend

Definition at line 275 of file mlBackgroundTask.h.

References BackgroundTaskManagerWorkerFunc, and status().

Referenced by BackgroundTaskManagerWorkerFunc.


The documentation for this class was generated from the following file: